• 目的

        了解线程池的知识后,写个线程池实例,熟悉多线程开发,建议看jdk线程池源码,跟大师比,才知道差距啊O(∩_∩)O


  • 线程池类
 package thread.pool2;

 import java.util.LinkedList;

 public class ThreadPool {
//最大线程数
private int maxCapacity;
//初始线程数
private int initCapacity;
//当前线程数
private int currentCapacity;
//线程池需要执行的任务
private LinkedList<Task> tasks;
//当前处于等待的线程数
private int waitThreadNum = 0;
//线程池中线程数超过初始数量时,此时有线程执行完任务,但是没有后续的任务执行,则会等待一段时间后,该线程才销毁
//destroyTime小于或等于0时,线程立即消费,大于0,则等待设置的时间
private int destroyTime = 0; public ThreadPool(int initCapacity,int maxCapacity, int destroyTime) {
if(initCapacity > maxCapacity) {
//初始线程数不能超过最大线程数,当然此处可以抛出异常,提示不允许这么设置
initCapacity = maxCapacity;
}
this.maxCapacity = maxCapacity;
this.initCapacity = initCapacity;
this.currentCapacity = initCapacity;
this.tasks = new LinkedList<Task>();
this.waitThreadNum = initCapacity;
this.destroyTime = destroyTime;
}
/**
* 向线程池中添加任务,如果线程数不够,则增加线程数,但线程数总量不能超过给定的最大线程数
* @param task
*/
public synchronized void addTask(Task task) {
tasks.add(task);
addThread();
notifyAll();
}
/**
* 从线程池中取出任务,如果没有任务,则当前线程处于等待状态
* @return
* @throws InterruptedException
*/
public synchronized Task getTask() throws InterruptedException {
while(tasks.isEmpty()) {
wait();
}
//取出第一个任务的同时将第一个任务移除
return tasks.pollFirst();
}
/**
* 判断线程池中任务列表是否为空
* @return
*/
public synchronized boolean isEmpty() {
return tasks.isEmpty();
}
/**
* 活跃线程数加1
*/
public synchronized void addWaitThreadNum(int num) {
waitThreadNum += num;
}
/**
* 活跃线程数减1
*/
public synchronized void reduceWaitThreadNum(int num) {
waitThreadNum -= num;
} /**
* 启动线程池
*/
public void execute() {
System.out.println(initCapacity);
for(int i = 0; i < initCapacity; i++) {
(new Thread(new InnerThread(this, "thread"+ i))).start();
}
}
/**
* 如果当前线程数大于初始线程数,则关闭当前线程,否则当前线程处于等待状态
* @return
* @throws InterruptedException
*/
public synchronized boolean waitOrClose(int tmp) throws InterruptedException {
System.out.println(currentCapacity + ":" + initCapacity);
//线程退出前,等待一段时间,防止线程频繁创建和销毁线程
if(destroyTime > 0) {
wait(destroyTime);
}
if(currentCapacity > initCapacity && tasks.isEmpty()) {
currentCapacity--;
System.out.println("任务执行完后,当前线程数:" + currentCapacity);
return false;
}
System.out.println("线程等待结束");
addWaitThreadNum(tmp);
wait();
return true;
} /**
* 当线程池内线程数不够时,如果有任务在等待处理,同时当前线程都处于非等待状态,
* 则增加线程池中线程数,但不能超过线程池中最大线程数
*/
public synchronized void addThread() {
System.out.println("当前线程数:" + currentCapacity + "最大线程数:" + maxCapacity + "等待线程数" + waitThreadNum);
if(currentCapacity < maxCapacity && waitThreadNum == 0) {
//每添加一个线程,当前线程数加1
currentCapacity++;
//每添加一个线程,相当于线程池中多了一个等待的线程
waitThreadNum++;
System.out.println("当前线程数为:" + currentCapacity);
new Thread(new InnerThread(this, "thread" + (currentCapacity-1))).start();
}
}
/**
* 线程池中单个线程对象
* @author yj
*
*/
private class InnerThread implements Runnable { private ThreadPool threadPool;
private String threadName; public InnerThread(ThreadPool threadPool, String threadName) {
this.threadPool = threadPool;
this.threadName = threadName;
} @Override
public void run() {
try {
while(true){
int addWait = 0;
int resuceWait = 1;
//不等于空,则处理任务
while(!threadPool.isEmpty()) {
threadName = Thread.currentThread().getName();
reduceWaitThreadNum(resuceWait);
Task task = threadPool.getTask();
task.execute(threadName);
try {
Thread.sleep(9000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(threadName + "对"+task.getTaskName()+"+任务进行了处理");
//只有处理任务后回到等待状态的线程才将waitThreadNum加1
addWait = 1;
//如果不跳出循环,则等待线程数不减少
resuceWait = 0;
}
//等于空,则等待任务或关闭当前线程
if(threadPool.waitOrClose(addWait)) {
System.out.println(threadName + "处于等待状态");
continue;
}
//关闭线程
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

  • 任务类
 package thread.pool2;

 public class Task{

     private String taskName;

     public String getTaskName() {
return taskName;
} public Task(String taskName) {
this.taskName = taskName;
} public void execute(String threadName) {
System.out.println(threadName + "开始执行任务为" + taskName);
/*try {
Thread.sleep(9000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println(threadName + "执行" + taskName + "任务完成");
} }

  • 测试类
 package thread.pool2;

 public class ThreadPoolTest {

     public static void main(String[] args) {
ThreadPool threadPool = new ThreadPool(3, 10, 1100);
threadPool.execute();
for(int i = 0; i < 50; i++) {
int random = (int) (Math.random() * 1000);
threadPool.addTask(new Task("task"+random));
/*try {
//每个1秒向线程池中添加任务
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
} }

java线程池实例的更多相关文章

  1. JAVA四种线程池实例

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗?   Java   1 2 3 4 5 6 7 new Thread(new Runnable() {        ...

  2. java 线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用。

    转载请声明出处谢谢!http://www.cnblogs.com/linguanh/ 这里主要使用Executors中的4种静态创建线程池实例方法中的 newFixedThreadPool()来举例讲 ...

  3. Java线程池使用说明

    Java线程池使用说明 转自:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极 ...

  4. 四种Java线程池用法解析

    本文为大家分析四种Java线程池用法,供大家参考,具体内容如下 http://www.jb51.net/article/81843.htm 1.new Thread的弊端 执行一个异步任务你还只是如下 ...

  5. Java线程池应用

    Executors工具类用于创建Java线程池和定时器. newFixedThreadPool:创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThread ...

  6. [转 ]-- Java线程池使用说明

    Java线程池使用说明 原文地址:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1. ...

  7. Java线程池学习

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

  8. java线程池的使用与详解

    java线程池的使用与详解 [转载]本文转载自两篇博文:  1.Java并发编程:线程池的使用:http://www.cnblogs.com/dolphin0520/p/3932921.html   ...

  9. java线程池分析和应用

    比较 在前面的一些文章里,我们已经讨论了手工创建和管理线程.在实际应用中我们有的时候也会经常听到线程池这个概念.在这里,我们可以先针对手工创建管理线程和通过线程池来管理做一个比较.通常,我们如果手工创 ...

随机推荐

  1. openstack(Pike 版)集群部署(三)--- Glance 部署

    一.介绍 参照官网部署:https://docs.openstack.org/glance/queens/install/ 继续上一博客进行部署:http://www.cnblogs.com/weij ...

  2. json 相关知识

    一:json标准格式: 标准JSON的合法符号:{(左大括号)  }(右大括号)  "(双引号)  :(冒号)  ,(逗号)  [(左中括号)  ](右中括号) JSON字符串:特殊字符可在 ...

  3. oracle中job定时任务96

    .INTERVAL参数常用值示例 每天午夜12点            ''TRUNC(SYSDATE + 1)'' 每天早上8点30分         ''TRUNC(SYSDATE + 1) +  ...

  4. Angular之响应式表单 ( Reactive Forms )

    项目结构 一 首页 ( index.html ) <!doctype html> <html lang="en"> <head> <met ...

  5. HDU 3251 Being a Hero(最小割+输出割边)

    Problem DescriptionYou are the hero who saved your country. As promised, the king will give you some ...

  6. nSamplesPerSec和nAvgBytesPerSec

    nAvgBytesPerSec 意思是每秒多少字节,你的每个sample位深(精度/bitpersample)是16bit,就是2字节,nSamplesPerSec一秒44100个sample(441 ...

  7. 489. Robot Room Cleaner扫地机器人

    [抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...

  8. GridView和DataFormatString 日期格式 精确小数点后位数

    如果DataFormatString无效,请添加属性 HtmlEncode = "false" --------------------------------------- Da ...

  9. java 线程Thread 技术--线程状态与同步问题

    线程技术第三篇: 线程的状态: 1. 创建状态: 当用new 操作符创建一个新的线程对象时,该线程就处于创建状态,系统不为它分配资源 2.可运行状态:当线程调用start 方法将为线程分配必须的系统资 ...

  10. java NIO Buffer 详解(1)

    1.java.io  最为核心的概念是流(stream),面向流的编程,要么输入流要么输出流,二者不可兼具: 2.java.nio 中拥有3个核心概念: Selector Channel, Buffe ...