本文可作为传智播客《张孝祥-Java多线程与并发库高级应用》视频的学习记录。

为什么需要并发池

之前写并发的时候

new Thread(new Runnable(){
    public void run{
    //....
    }
}).start();

没有什么问题呀,为什么需要并发池呢?

我们拿tomcat来举个例子。每次我们对服务器做一个请求,tomcat就得分出一个线程来处理我们的事。那么是不是,我们每次发一个请求,tomcat就像上面那样启动一个线程,等到run方法执行完了,这个线程也就死了。如果真是这样,来100个请求,tomcat就得产生100个线程,然后100个线程都死了,下一个请求一来,tomcat还得再创建一个线程。(创建一个线程还是有时间花费的。)

真是情况是这样的,tomcat就像酒店里的一个领班,她手底下有10个服务员,每次来一个顾客,就从那10个服务员里找一个人来和顾客交流。等服务员和顾客交流完了,那个服务再回到他们原来10个人呆的地方,等待她自己的下一位顾客。如果酒店里同时涌进来15个顾客。那么就是10个服务员同时出动,当然还有5个顾客没有人招待,怎么办?等着呗,等前面某位服务员忙完了,再来招呼那5个被阻塞的顾客。

那10个服务员就可以理解为一个"服务池"。

这样说,线程池存在的必要性大家应该都懂了吧。

java5之后的线程池

java5后的线程池主要就是指

java.util.concurrent  Interface ExecutorService

它一般使用

java.util.concurrent Class Executors 这个类来产生。

我们看看不同的产生方式。

1

ExecutorService threadPool = Executors.newFixedThreadPool(3);

这种线程池里,只有3个线程。

2

ExecutorService threadPool = Executors.newCachedThreadPool();

线程池里的线程的数量会根据任务数而动态分配。

3

ExecutorService threadPool = Executors.newSingleThreadExecutor();

线程池里只有一个线程,如果当前执行任务的线程中断了,池子会自己创建一个新线程替代它。

4

ScheduledExecutorService threadPool = Executors.newScheduledThreadPool();

创建一个可安排在给定延迟后运行命令或者定期地执行的线程池。

newFixedThreadPool

package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest2 {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        for (int i = 1; i <= 5; i++) {
            final int task = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j <= 5; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()
                                + " is looping of " + j + " for  task of "
                                + task);
                    }
                }
            });
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("all of 10 tasks have committed! ");
        //这里是关闭所有线程 有可能上面的任务还没有执行完,
        //因此上面让主线程睡2秒 保证任务执行完毕
        threadPool.shutdownNow();
    }
}

看看结果

pool-1-thread-1 is looping of 1 for  task of 1

pool-1-thread-2 is looping of 1 for  task of 2

pool-1-thread-3 is looping of 1 for  task of 3

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-1 is looping of 2 for  task of 1

pool-1-thread-1 is looping of 3 for  task of 1

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-3 is looping of 4 for  task of 3

pool-1-thread-2 is looping of 4 for  task of 2

pool-1-thread-1 is looping of 4 for  task of 1

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 5 for  task of 3

pool-1-thread-1 is looping of 1 for  task of 4

pool-1-thread-2 is looping of 1 for  task of 5

pool-1-thread-1 is looping of 2 for  task of 4

pool-1-thread-2 is looping of 2 for  task of 5

pool-1-thread-2 is looping of 3 for  task of 5

pool-1-thread-1 is looping of 3 for  task of 4

pool-1-thread-1 is looping of 4 for  task of 4

pool-1-thread-2 is looping of 4 for  task of 5

pool-1-thread-2 is looping of 5 for  task of 5

pool-1-thread-1 is looping of 5 for  task of 4

all of 10 tasks have committed!

是task of1 2 3 运行完毕后4 5才出来的。为什么?因为池子里只有三个线程。

newCachedThreadPool

我们把产生线程池的代码换成如下:

ExecutorService threadPool = Executors.newCachedThreadPool();

结果

pool-1-thread-1 is looping of 1 for  task of 1

pool-1-thread-2 is looping of 1 for  task of 2

pool-1-thread-3 is looping of 1 for  task of 3

pool-1-thread-4 is looping of 1 for  task of 4

pool-1-thread-5 is looping of 1 for  task of 5

pool-1-thread-5 is looping of 2 for  task of 5

pool-1-thread-4 is looping of 2 for  task of 4

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-1 is looping of 2 for  task of 1

pool-1-thread-1 is looping of 3 for  task of 1

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-4 is looping of 3 for  task of 4

pool-1-thread-5 is looping of 3 for  task of 5

pool-1-thread-5 is looping of 4 for  task of 5

pool-1-thread-4 is looping of 4 for  task of 4

pool-1-thread-3 is looping of 4 for  task of 3

pool-1-thread-2 is looping of 4 for  task of 2

pool-1-thread-1 is looping of 4 for  task of 1

pool-1-thread-5 is looping of 5 for  task of 5

pool-1-thread-4 is looping of 5 for  task of 4

pool-1-thread-3 is looping of 5 for  task of 3

pool-1-thread-2 is looping of 5 for  task of 2

pool-1-thread-1 is looping of 5 for  task of 1

all of 10 tasks have committed!

看到了吧 5个任务都启动了。因为线程池里动态给加线程的。

newSingleThreadExecutor

我们把产生线程池的代码换成如下:

ExecutorService threadPool = Executors.newSingleThreadExecutor();

pool-1-thread-1 is looping of 1 for  task of 1

pool-1-thread-1 is looping of 2 for  task of 1

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-1 is looping of 5 for  task of 1

pool-1-thread-1 is looping of 1 for  task of 2

pool-1-thread-1 is looping of 2 for  task of 2

pool-1-thread-1 is looping of 3 for  task of 2

pool-1-thread-1 is looping of 4 for  task of 2

pool-1-thread-1 is looping of 5 for  task of 2

pool-1-thread-1 is looping of 1 for  task of 3

pool-1-thread-1 is looping of 2 for  task of 3

pool-1-thread-1 is looping of 3 for  task of 3

pool-1-thread-1 is looping of 4 for  task of 3

pool-1-thread-1 is looping of 5 for  task of 3

pool-1-thread-1 is looping of 1 for  task of 4

pool-1-thread-1 is looping of 2 for  task of 4

pool-1-thread-1 is looping of 3 for  task of 4

pool-1-thread-1 is looping of 4 for  task of 4

pool-1-thread-1 is looping of 5 for  task of 4

pool-1-thread-1 is looping of 1 for  task of 5

pool-1-thread-1 is looping of 2 for  task of 5

pool-1-thread-1 is looping of 3 for  task of 5

pool-1-thread-1 is looping of 4 for  task of 5

pool-1-thread-1 is looping of 5 for  task of 5

all of 10 tasks have committed!

里面只有一个线程,自然看上去就是顺序执行了。

另外关于这个"如果当前执行任务的线程中断了,池子会自己创建一个新线程替代它"

我还不知道具体的例子。

newScheduledThreadPool

ScheduledExecutorService schedulePool = Executors.newScheduledThreadPool(1);
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    public class ThreadPoolTest {
        public static void main(String[] args) {
            ScheduledExecutorService schedulePool = Executors.newScheduledThreadPool(1);
            // 5秒后执行任务
            schedulePool.schedule(new Runnable() {
                public void run() {
                    System.out.println("爆炸");
                }
            }, 5, TimeUnit.SECONDS);
            // 5秒后执行任务,以后每2秒执行一次
            schedulePool.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    System.out.println("爆炸");
                }
            }, 5, 2, TimeUnit.SECONDS);  

        try {
            Thread.sleep(10*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        schedulePool.shutdown();
        }
    }  

第四个例子参见

http://blog.csdn.net/dlf123321/article/details/42741743

传统定时器

参考资料

http://blog.csdn.net/ghsau/article/details/7443324

java5后的并发池的更多相关文章

  1. Java5中的线程池实例讲解

    Java5增加了新的类库并发集java.util.concurrent,该类库为并发程序提供了丰富的API多线程编程在Java 5中更加容易,灵活.本文通过一个网络服务器模型,来实践Java5的多线程 ...

  2. java--加强之 Java5的线程并发库

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9945499 01. 传统线程技术回顾 创建线程的两种传统方式: 1.在Thread子类覆盖的r ...

  3. java5引入的并发编程库

    java5之后引入了java.util.concurrent包,专门用于解决java多线程问题.   AtomicInteger用于解决原子性操作(i++,i--的问题): AtomicInteger ...

  4. Tomcat配置https后,并发较大时,频繁超时情况。

    tomcat配置ssl后,出现频繁的访问超时情况. 通过脚本(感谢UCloud的技术支持 金晓帆-): netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a i ...

  5. cell选中后进入重用池出来选中状态消失

    #import "XXViewController.h" @interface XXViewController ()<UITableViewDelegate,UITable ...

  6. Java5 并发学习

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过 Executor来启动线程比用Thread的start()更好.在新特征 ...

  7. 转:【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)

      Executor框架简介 在Java5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.coc ...

  8. 【Java并发编程】:并发新特性—Executor框架与线程池

    Executor框架简介 在Java5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.cocur ...

  9. 并发新特性—Executor框架与线程池

    http://blog.csdn.net/ns_code/article/details/17465497 Executor框架简介 在Java5之后,并发编程引入了一堆新的启动.调度和管理线程的AP ...

随机推荐

  1. Python 一个奇特的引用设定

    def f(x): print 'original' if x > 0: return f(x-1) return 0 g = f def f(x): print 'new' return x ...

  2. [python]mysql数据缓存到redis中 取出时候编码问题

    描述: 一个web服务,原先的业务逻辑是把mysql查询的结果缓存在redis中一个小时,加快请求的响应. 现在有个问题就是根据请求的指定的编码返回对应编码的response. 首先是要修改响应的bo ...

  3. Android开发艺术探索笔记——第一章:Activity的生命周期和启动模式

    Android开发艺术探索笔记--第一章:Activity的生命周期和启动模式 怀着无比崇敬的心情翻开了这本书,路漫漫其修远兮,程序人生,为自己加油! 一.序 作为这本书的第一章,主席还是把Activ ...

  4. ArrayList、HashMap、HashSet源码总结

    ArrayList: 1. ArrayList是List接口的大小可变数组的实现,此实现是不同步的. 2. ArrayList内部使用类型为Object[]的数组存储元素. 3. ArrayList默 ...

  5. linux下的环境变量

    环境变量有时候要查找,但是经常忘记有哪些文件,现在做一个总结: /etc/profile                 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/e ...

  6. 指令汇B新闻客户端开发(五) ShareSdk的使用

    ShareSdk是一个分享按钮的开源框架,我们首先可以去mob的官网下载这个控件.mob官网,然后找到sdk下载那一栏, 下载下来之后点击这个.jar文件就会有一个弹窗,填写自己的应用包名和要哪些分享 ...

  7. 6.4、Android Studio的GPU Monitor

    Android Monitor包含GPU Monitor,它将可视化的显示渲染窗体的时间.GPU Monitor可以帮助你: 1. 迅速查看UI窗体生成 2. 辨别是否渲染管道超出使用线程时间 在GP ...

  8. linux中查看现在使用的shell是ksh还是bash?以及怎样修改?

    查看系统支持的shell: cat  /etc/shells 查看现在使用的shell:  修改默认shell: 另外,修改了系统默认shell之后不会立即生效,之后再次登录系统修改的shell才会生 ...

  9. tomcat中Servlet的工作机制

    在研究Servlet在tomcat中的工作机制前必须先看看Servlet规范的一些重要的相关规定,规范提供了一个Servlet接口,接口中包含的重要方法是init.service.destroy等方法 ...

  10. Collections类解析

    最常用的排序: 需要实现Comparable接口 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它 ...