为了账号安全,请及时绑定邮箱和手机立即绑定

多线程:(六)初入线程池

标签:
Java

这两天有点困、累,可能是公司任务太少,一松懈,疲劳都上来了,也可能是自己老了吧,O(∩_∩)O哈哈~


概述一下子:

根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。

线程池减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。

上代码,读逻辑,Go!

/**
 * 十个任务,每个任务执行十次
 * @author yang
 */
public class ThreadPoolTest {

    public static void main(String[] args) {
        //固定线程数量的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        //线程池的大小依赖于操作系统(或者说JVM)
        // ExecutorService threadPool = Executors.newCachedThreadPool();
        //单个线程的线程池:只有一个线程在工作,如果这个线程异常结束,则会立刻有一个新线程替代它
        // ExecutorService threadPool = Executors.newSingleThreadExecutor();

        for (int i = 1; i <= 10; i++) {
            final int task = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j <= 10; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for  task of " + task);
                    }
                }
            });
        }
        // threadPool.shutdown();       //结束线程池

        //附带一个定时任务,O(∩_∩)O哈哈~
        Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("bombing!");

            }
        }, 6, 2, TimeUnit.SECONDS);
    }

}

部分打印结果(因为线程池里开了3个线程,所以3个任务同时执行完十次以后,再接着往后同时执行3个任务):
pool-1-thread-2 is looping of 1 for  task of 2
pool-1-thread-1 is looping of 1 for  task of 1
pool-1-thread-3 is looping of 1 for  task of 3
pool-1-thread-1 is looping of 2 for  task of 1
pool-1-thread-3 is looping of 2 for  task of 3
pool-1-thread-2 is looping of 2 for  task of 2
pool-1-thread-2 is looping of 3 for  task of 2
pool-1-thread-3 is looping of 3 for  task of 3
pool-1-thread-1 is looping of 3 for  task of 1
pool-1-thread-1 is looping of 4 for  task of 1
pool-1-thread-2 is looping of 4 for  task of 2
pool-1-thread-3 is looping of 4 for  task of 3
pool-1-thread-3 is looping of 5 for  task of 3
pool-1-thread-1 is looping of 5 for  task of 1
pool-1-thread-2 is looping of 5 for  task of 2
pool-1-thread-3 is looping of 6 for  task of 3
pool-1-thread-2 is looping of 6 for  task of 2
pool-1-thread-1 is looping of 6 for  task of 1
pool-1-thread-2 is looping of 7 for  task of 2
pool-1-thread-1 is looping of 7 for  task of 1
pool-1-thread-3 is looping of 7 for  task of 3
pool-1-thread-2 is looping of 8 for  task of 2
pool-1-thread-1 is looping of 8 for  task of 1
pool-1-thread-3 is looping of 8 for  task of 3
pool-1-thread-1 is looping of 9 for  task of 1
pool-1-thread-3 is looping of 9 for  task of 3
pool-1-thread-2 is looping of 9 for  task of 2
pool-1-thread-3 is looping of 10 for  task of 3
pool-1-thread-2 is looping of 10 for  task of 2
pool-1-thread-1 is looping of 10 for  task of 1
pool-1-thread-3 is looping of 1 for  task of 4
pool-1-thread-1 is looping of 1 for  task of 5
pool-1-thread-2 is looping of 1 for  task of 6
pool-1-thread-1 is looping of 2 for  task of 5
pool-1-thread-3 is looping of 2 for  task of 4
pool-1-thread-2 is looping of 2 for  task of 6

好了,简洁总结,深刻理解。有不对的地方,欢迎指出,在此谢过。

原文:多线程:(六)初入线程池


那时年少轻狂,把喜欢当做筹码,才敢肆无忌惮的任性。
----那时年少轻狂

点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消