「这是我参与11月更文挑战的第15天,活动详情查看:2021最后一次更文挑战」
线程的状态:
线程池:
线程池-- 代码实现:
public class MyThreadPoolDemo {
public static void main(String[] args) throws InterruptedException {
// 创建一个默认的线程池 对象
ExecutorService executorService = Executors.newCachedThreadPool();
// Executors -- 可以帮助我们创建线程池对象
// ExecutorService -- 可以帮助我们控制线程池
executorService.submit(()->{
System.out.println(Thread.currentThread().getName() + "在执行了");
});
// Thread.sleep(2000);
executorService.submit(()->{
System.out.println(Thread.currentThread().getName() + "在执行了");
});
executorService.shutdown();
}
}
pool-1-thread-2在执行了
pool-1-thread-1在执行了
public class MyThreadPoolDemo {
public static void main(String[] args) throws InterruptedException {
// 创建一个默认的线程池 对象
ExecutorService executorService = Executors.newCachedThreadPool();
// Executors -- 可以帮助我们创建线程池对象
// ExecutorService -- 可以帮助我们控制线程池
executorService.submit(()->{
System.out.println(Thread.currentThread().getName() + "在执行了");
});
Thread.sleep(2000);
executorService.submit(()->{
System.out.println(Thread.currentThread().getName() + "在执行了");
});
executorService.shutdown();
}
}
pool-1-thread-1在执行了
pool-1-thread-1在执行了
创建指定上限的线程池
public class MyThreadPoolDemo2 {
public static void main(String[] args) {
// 参数不是初始值 而是 最大值
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.submit(()->{
System.out.println(Thread.currentThread().getName() + "在执行了");
});
executorService.submit(()->{
System.out.println(Thread.currentThread().getName() + "在执行了");
});
executorService.shutdown();
}
}
public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "在执行");
}
}
public class MyThreadPoolDemo3 {
public static void main(String[] args) {
// 参数1:核心线程数量
// 参数2: 最大线程数
// 参数3: 空闲线程最大存活时间
// 参数4:时间单位
// 参数5:任务队列
// 参数6: 创建线程工厂
// 参数7: 任务的拒绝策略
ThreadPoolExecutor pool = new ThreadPoolExecutor(
2,
5,
2, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
pool.submit(new MyRunnable());
pool.submit(new MyRunnable());
pool.shutdown();
}
}
创建线程池对象:
public class MyThreadPoolDemo3 {
public static void main(String[] args) {
// 参数1:核心线程数量
// 参数2: 最大线程数
// 参数3: 空闲线程最大存活时间 (2s 不执行)
// 参数4: 时间单位
// 参数5: 任务队列 (让任务在队列中等着,等有线程空闲了,再从这个队列中获取任务并执行)
// 参数6: 创建线程工厂 (按照默认的方式创建线程对象)
// 参数7: 任务的拒绝策略 (1.什么时候拒绝任务 (当提交的任务 > 池子中最大线程数量 + 队列容量)
// 2.如何拒绝 ())
ThreadPoolExecutor pool = new ThreadPoolExecutor(
2,
5,
2,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
pool.submit(new MyRunnable());
pool.submit(new MyRunnable());
pool.shutdown();
}
}
public class Money {
public static int money = 100000;
}
public class Mythread1 extends Thread{
@Override
public void run() {
while (Money.money == 100000){
}
System.out.println("结婚基金已经不是十万了");
}
}
public class Mythread2 extends Thread{
@Override
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Money.money = 90000;
}
}
public class Demo {
public static void main(String[] args) {
Mythread1 t1 = new Mythread1();
t1.setName("小路同学");
t1.start();
Mythread2 t2 = new Mythread2();
t2.setName("小皮同学");
t2.start();
}
}
public class Money {
public static volatile int money = 100000;
}
第二种方式:同步代码块
public class Money {
public static Object lock =new Object();
public static int money = 100000;
}
public class Mythread1 extends Thread{
@Override
public void run() {
while (true){
synchronized (Money.lock){
if (Money.money != 100000){
System.out.println("结婚基金已经不是十万了");
break;
}
}
}
}
}
public class Mythread2 extends Thread{
@Override
public void run() {
synchronized (Money.lock){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Money.money = 90000;
}
}
}
public class Demo {
public static void main(String[] args) {
Mythread1 t1 = new Mythread1();
t1.setName("小路同学");
t1.start();
Mythread2 t2 = new Mythread2();
t2.setName("小皮同学");
t2.start();
}
}