继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

那时年少轻狂
关注TA
已关注
手记 6
粉丝 18
获赞 71

这两天有点困、累,可能是公司任务太少,一松懈,疲劳都上来了,也可能是自己老了吧,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

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

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


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

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP