java的线程池学习

学习来自慕课网的《剑指Java面试-Offer直通车》;

先学习一下代码:

//(1)创建无限大小的线程池
public class NewCachedOtherTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int x = 0; x < 10; x++) {
            Thread.sleep(200);
            int index = x;
            executorService.submit(()->{
                System.out.println(Thread.currentThread().getName() + ", x=" + index);
                try {
                    Thread.sleep(1000); //添加延时函数,表示该线程没有执行完,这样在线程池中,需要创建一个新的线程。
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();
    }
}

//运行结果,加了延时后,前面的线程没有运行完,会创建新的线程
pool-1-thread-1, x=0
pool-1-thread-2, x=1
pool-1-thread-3, x=2
pool-1-thread-4, x=3
pool-1-thread-5, x=4
pool-1-thread-1, x=5
pool-1-thread-2, x=6
pool-1-thread-3, x=7
pool-1-thread-4, x=8
pool-1-thread-5, x=9
//创建固定大小的线程池
public class NewFixedTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int x = 0; x < 10; x++) {
            Thread.sleep(200);
            int index = x;
            executorService.submit(()->{
                System.out.println(Thread.currentThread().getName() + ", x=" + index);
            });
        }
        executorService.shutdown();
    }
}

//运行结果,可以看到,只由3个线程
pool-1-thread-1, x=0
pool-1-thread-2, x=1
pool-1-thread-3, x=2
pool-1-thread-1, x=3
pool-1-thread-2, x=4
pool-1-thread-3, x=5
pool-1-thread-1, x=6
pool-1-thread-2, x=7
pool-1-thread-3, x=8
pool-1-thread-1, x=9
//创建单线程的线程池
public class NewSingleTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int x = 0; x < 10; x++) {
            Thread.sleep(200);
            int index = x;
            executorService.submit(()->{
                System.out.println(Thread.currentThread().getName() + ", x=" + index);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();
    }
}

//运行结果,每隔一秒钟打印一个
pool-1-thread-1, x=0
pool-1-thread-1, x=1
pool-1-thread-1, x=2
pool-1-thread-1, x=3
pool-1-thread-1, x=4
pool-1-thread-1, x=5
pool-1-thread-1, x=6
pool-1-thread-1, x=7
pool-1-thread-1, x=8
pool-1-thread-1, x=9
// 定时调度线程池
public class NewScheduledTest {
    public static void main(String[] args) throws InterruptedException {
        //创建具备3个线程大小的定时调度线程池
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
        long start = System.currentTimeMillis();
        for (int x = 0; x < 5; x++) {
            Thread.sleep(200);
            int index = x;
            executorService.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ", x=" + index + ", time=" +
                            ( System.currentTimeMillis() - start) );
                }
            },3,2, TimeUnit.SECONDS);  //使用的时一个秒的单位,表示第3秒后开始执行,而后每2秒执行一次。
        }
   //     executorService.shutdown();   不能关闭线程;
    }
}

//运行结果:
pool-1-thread-1, x=0, time=3204
pool-1-thread-2, x=1, time=3405
pool-1-thread-3, x=2, time=3606
pool-1-thread-1, x=3, time=3806
pool-1-thread-2, x=4, time=4007
pool-1-thread-3, x=0, time=5205
pool-1-thread-1, x=1, time=5406
pool-1-thread-2, x=2, time=5607
pool-1-thread-3, x=3, time=5808
pool-1-thread-1, x=4, time=6009
pool-1-thread-2, x=0, time=7205
pool-1-thread-3, x=1, time=7405
pool-1-thread-1, x=2, time=7606
pool-1-thread-2, x=3, time=7807
pool-1-thread-3, x=4, time=8009

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值