您的位置:首页 > 编程语言 > Java开发

JAVA 并发编程-线程池(七)

2015-07-27 19:21 633 查看

线程池的作用:

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

为什么要用线程池:

 1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下 Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是ExecutorService。 要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,很有可能配置的线程池不是较优的,因此在Executors类里面提供了一些静态工厂,生成一些常用的线程池。 

1.newSingleThreadExecutor

创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务。如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。 

2.newFixedThreadPool

创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。 

3. newCachedThreadPool

创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。 

4.newScheduledThreadPool(int corePoolSize)

(定时及周期性执行任务,稍后会详细介绍)创建corePoolSize大小的线程池。此线程池支持定时以及周期性执行任务的需求。 newSingleThreadExecutor:
public class ThreadPoolTest {

/**
* @param args
*/
public static void main(String[] args) {
//固定线程池大小
// ExecutorService threadPool = Executors.newFixedThreadPool(3);
//可缓存的线程池
//ExecutorService threadPool = Executors.newCachedThreadPool();
//单线程的线程池
ExecutorService threadPool = Executors.newSingleThreadExecutor();

//向线程池中放入10个任务,每次只能为一个任务服务(单线程的线程池)
for(int i=1;i<=10;i++){
final int task = i;
//执行任务(可以与new Thread(new Runnable(){})相比,相当于10个任务创建10个线程,而此时只有一个线程)
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);
}
}
});
}
//10个任务放入线程池后,才会执行下面的代码,打印出此提示信息,说明已经放入了10个任务
System.out.println("all of 10 tasks have committed! ");

//当线程池内存在线程时,是不会停止的,想要停止使用如下语句
//threadPool.shutdownNow();

//定时执行任务
// Executors.newScheduledThreadPool(3).scheduleAtFixedRate(
// new Runnable(){
// @Override
// public void run() {
// System.out.println("bombing!");
//
// }},
// 6,
// 2,
// TimeUnit.SECONDS);
}

}

执行结果:
all of 10 tasks havecommitted! pool-1-thread-1 islooping of 1 for  task of 1pool-1-thread-1 islooping of 2 for  task of 1pool-1-thread-1 islooping of 3 for  task of 1pool-1-thread-1 islooping of 4 for  task of 1pool-1-thread-1 islooping of 5 for  task of 1pool-1-thread-1 islooping of 6 for  task of 1pool-1-thread-1 islooping of 7 for  task of 1pool-1-thread-1 islooping of 8 for  task of 1pool-1-thread-1 islooping of 9 for  task of 1pool-1-thread-1 islooping of 10 for  task of 1pool-1-thread-1 islooping of 1 for  task of 2pool-1-thread-1 islooping of 2 for  task of 2pool-1-thread-1 islooping of 3 for  task of 2pool-1-thread-1 islooping of 4 for  task of 2  newFixedThreadPool:
执行结果: 
all of 10 tasks havecommitted! pool-1-thread-1 islooping of 1 for  task of 1pool-1-thread-3 islooping of 1 for  task of 3pool-1-thread-2 islooping of 1 for  task of 2pool-1-thread-3 islooping of 2 for  task of 3pool-1-thread-2 islooping of 2 for  task of 2pool-1-thread-1 islooping of 2 for  task of 1pool-1-thread-3 islooping of 3 for  task of 3pool-1-thread-2 islooping of 3 for  task of 2pool-1-thread-1 islooping of 3 for  task of 1pool-1-thread-1 islooping of 4 for  task of 1pool-1-thread-2 islooping of 4 for  task of 2pool-1-thread-3 islooping of 4 for  task of 3pool-1-thread-1 islooping of 5 for  task of 1pool-1-thread-2 islooping of 5 for  task of 2pool-1-thread-3 islooping of 5 for  task of 3pool-1-thread-1 islooping of 6 for  task of 1pool-1-thread-2 islooping of 6 for  task of 2pool-1-thread-3 islooping of 6 for  task of 3pool-1-thread-3 islooping of 7 for  task of 3pool-1-thread-1 islooping of 7 for  task of 1pool-1-thread-2 islooping of 7 for  task of 2pool-1-thread-3 islooping of 8 for  task of 3pool-1-thread-1 islooping of 8 for  task of 1pool-1-thread-2 islooping of 8 for  task of 2pool-1-thread-1 islooping of 9 for  task of 1pool-1-thread-3 islooping of 9 for  task of 3pool-1-thread-2 islooping of 9 for  task of 2pool-1-thread-3 islooping of 10 for  task of 3pool-1-thread-2 islooping of 10 for  task of 2pool-1-thread-1 islooping of 10 for  task of 1pool-1-thread-1 islooping of 1 for  task of 6pool-1-thread-2 islooping of 1 for  task of 5pool-1-thread-3 islooping of 1 for  task of 4pool-1-thread-3 islooping of 2 for  task of 4pool-1-thread-1 islooping of 2 for  task of 6pool-1-thread-2 islooping of 2 for  task of 5 newCachedThreadPool:
执行结果: all of 10 tasks havecommitted! pool-1-thread-5 islooping of 1 for  task of 5pool-1-thread-1 islooping of 1 for  task of 1pool-1-thread-4 islooping of 1 for  task of 4pool-1-thread-2 islooping of 1 for  task of 2pool-1-thread-3 islooping of 1 for  task of 3pool-1-thread-6 islooping of 1 for  task of 6pool-1-thread-10 islooping of 1 for  task of 10pool-1-thread-7 islooping of 1 for  task of 7pool-1-thread-8 islooping of 1 for  task of 8pool-1-thread-9 islooping of 1 for  task of 9pool-1-thread-1 islooping of 2 for  task of 1pool-1-thread-3 islooping of 2 for  task of 3pool-1-thread-5 islooping of 2 for  task of 5pool-1-thread-2 islooping of 2 for  task of 2pool-1-thread-6 islooping of 2 for  task of 6pool-1-thread-4 islooping of 2 for  task of 4pool-1-thread-7 islooping of 2 for  task of 7pool-1-thread-9 islooping of 2 for  task of 9pool-1-thread-10 islooping of 2 for  task of 10pool-1-thread-8 islooping of 2 for  task of 8pool-1-thread-1 islooping of 3 for  task of 1 

总结:

 创建一个单线程的线程池,根据输出结果我们可以知道在整个运行过程中共有唯一的一个线程创建,只有当前的任务完成后,这个线程才会给其他任务提供服务。创建固定大小的线程池,以上demo是以3个线程为例,那么线程池中会创建3个线程,每个线程完成各自的任务,只有各自任务完成后,才会给其他的任务提供服务。创建一个可缓存的线程池,即需要几个线程,创建几个线程,每个线程服务完成各自的任务即可。 

附:

package com.tgb.hjy;

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

public class TestSingleThreadExecutor {

public static void main(String[] args) {

//创建一个可重用固定线程数的线程池--单线程的线程池
ExecutorService pool = Executors. newSingleThreadExecutor();
//创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
Thread t1 = new MyPoolThread();
Thread t2 = new MyPoolThread2();
Thread t3 = new MyPoolThread();
Thread t4 = new MyPoolThread();
Thread t5 = new MyPoolThread();
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//关闭线程池
pool.shutdown();
}
}
class MyPoolThread extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行。。。");
}
}
class MyPoolThread2 extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行2。。。");
}
}

执行结果:
pool-1-thread-1正在执行。。。pool-1-thread-1正在执行2。。。pool-1-thread-1正在执行。。。pool-1-thread-1正在执行。。。pool-1-thread-1正在执行。。。 以上这种使用线程池的方式,个人觉得存在歧义,虽然处理任务的线程一直为线程一(复用线程),但是在最初我们就创建了5个线程,这样并不能体现线程池的优势,因为我们并没有做到减少创建和销毁线程的次数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: