Java线程池详解

  为什么要用线程池呢?

  一是减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务;
  二是可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)。

  线程池的基本思想是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理。当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程对象所带来的性能开销,节省了系统的资源。

   可见,线程池的作用主要是限制系统中执行线程的数量。   根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果。这是因为,线程少了会浪费系统资源,线程多了会造成系统拥挤、效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了;否则进入等待队列。

  Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具真正的线程池接口是ExecutorService。ThreadPoolExecutor是Executor接口的实现。为了简化操作,jdk5在concurrent包中为我们提供了线程池的工具类Executors来创建所需的线程池。

  1.固定大小的线程池  

package com.itszt.test3;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 线程池
 */
public class Test1 extends  Object{
    public static void main(String[] args) {
        //创建一个可重用,固定线程数的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        //创建实现了Runnable接口的类,如Thread
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        MyThread t3 = new MyThread();
        MyThread t4 = new MyThread();
        //将线程放入池中执行
        threadPool.execute(t1);
        threadPool.execute(t2);
        threadPool.execute(t3);
        threadPool.execute(t4);
        //关闭线程池
        threadPool.shutdown();
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"正在执行...");
    }
}

  执行结果如下:

pool-1-thread-1正在执行...
pool-1-thread-2正在执行...
pool-1-thread-1正在执行...
pool-1-thread-1正在执行...

  2.单任务线程池

  复用上述代码,将上例中创建线程池的代码改为:

ExecutorService threadPool = Executors.newSingleThreadExecutor();

  执行结果如下:

pool-1-thread-1正在执行...
pool-1-thread-1正在执行...
pool-1-thread-1正在执行...
pool-1-thread-1正在执行...

  3.可变尺寸的线程池

  改变创建线程池的方法:

ExecutorService threadPool = Executors.newCachedThreadPool();

  执行结果如下:

pool-1-thread-2正在执行...
pool-1-thread-1正在执行...
pool-1-thread-3正在执行...
pool-1-thread-4正在执行...

  4.延迟连接池

  修改创建线程池的方式:

ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
 MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
MyThread t4 = new MyThread();
//延迟执行
threadPool.schedule(t1,5, TimeUnit.MILLISECONDS);
threadPool.schedule(t2,5, TimeUnit.MILLISECONDS);
threadPool.schedule(t3,5, TimeUnit.MILLISECONDS);
threadPool.schedule(t4,5, TimeUnit.MILLISECONDS);
//关闭线程池
threadPool.shutdown();

  5.单任务延迟连接池

  修改创建线程池的方式:

ScheduledExecutorService threadPool = Executors.newSingleThreadScheduledExecutor();        

  6.自定义线程池

package com.itszt.test3;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * 自定义线程池
 */
public class Test2 {
    public static void main(String[] args) {
        //创建等待队列
        BlockingQueue bQueue = new ArrayBlockingQueue(20);
        //创建一个单线程执行程序,可安排在给定延迟时间后执行
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,2,TimeUnit.MILLISECONDS,bQueue);
        //创建实现了Runnable接口的类,如Thread
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        MyThread t3 = new MyThread();
        MyThread t4 = new MyThread();
        //将线程放入池中执行
        pool.execute(t1);
        pool.execute(t2);
        pool.execute(t3);
        pool.execute(t4);
        //关闭线程池
        pool.shutdown();
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"正在执行..."+System.currentTimeMillis());
    }
}  
posted @ 2018-03-24 20:49  奔跑在梦想的道路上  阅读(263)  评论(0编辑  收藏  举报