java 线程池 list,一个线程池的实现

CODE:package test.thread.pool1;

import java.util.ArrayList;

import java.util.List;

import test.thread.pool1.impl.MyWorkQueue;

/**

*

Title: 线程池管理器

*

Description:

*

Copyright: Copyright (c) 2005

*

Company:

* @author not attributable

* @version 1.0

*/

public class ThreadPoolManager {

/*最大线程数*/

private int threads_max_num;

/*最小线程数*/

private int threads_min_num;

/* 线程池线程增长步长 */

private int threads_increase_step = 5;

/* 任务工作队列 */

private IWorkQueue queue;

/* 线程池监视狗 */

private PoolWatchDog poolWatchDog ;

/* 队列线程 */

private Thread queueThread ;

/* 线程池 封装所有工作线程的数据结构 */

private List pool = new ArrayList();

/* 线程池中 封装所有钝化后的数据结构*/

private List passivePool = new ArrayList();

/* 空闲60秒 */

private static final long IDLE_TIMEOUT = 60000L;

/* 关闭连接池标志位 */

private boolean close = false;

/**

* 线程池管理器

* @param queue 任务队列

* @param threads_min_num 工作线程最小数

* @param threads_max_num 工作线程最大数

*/

public ThreadPoolManager(int threads_max_num

,int threads_min_num

,IWorkQueue queue){

this.threads_max_num = threads_max_num;

this.threads_min_num = threads_min_num;

this.queue = queue;

}

/**

* 线程池启动

*/

public void startPool(){

System.out.println("=== startPool..........");

poolWatchDog = new PoolWatchDog("PoolWatchDog");

poolWatchDog.setDaemon(true);

poolWatchDog.start();

System.out.println("=== startPool..........over");

}

/**

* 线程池销毁接口

*/

public void destoryPool(){

System.out.println("==========================DestoryPool starting ...");

this.close = true;

int pool_size = this.pool.size();

//中断队列线程

System.out.println("===Interrupt queue thread ... ");

queueThread.interrupt();

queueThread = null;

System.out.println("===Interrupt thread pool ... ");

Thread pool_thread = null;

for(int i=0; i

pool_thread = (Thread)pool.get(i);

if(pool_thread !=null

&& pool_thread.isAlive()

&& !pool_thread.isInterrupted()){

pool_thread.interrupt();

System.out.println("Stop pool_thread:"

+pool_thread.getName()+"[interrupt] "

+pool_thread.isInterrupted());

}

}//end for

if(pool != null){

pool.clear();

}

if(passivePool != null){

pool.clear();

}

try{

System.out.println("=== poolWatchDog.join() starting ...");

poolWatchDog.join();

System.out.println("=== poolWatchDog.join() is over ...");

}

catch(Throwable ex){

System.out.println("###poolWatchDog ... join method throw a exception ... "

+ex.toString());

}

poolWatchDog =null;

System.out.println("==============================DestoryPool is over ...");

}

public static void main(String[] args) throws Exception{

ThreadPoolManager threadPoolManager1 = new ThreadPoolManager(10,5,new MyWorkQueue(50,30000));

threadPoolManager1.startPool();

Thread.sleep(60000);

threadPoolManager1.destoryPool();

}

/**

* 线程池监视狗

*/

private class PoolWatchDog extends Thread{

public PoolWatchDog(String name){

super(name);

}

public void run(){

Thread workThread = null;

Runnable run = null;

//开启任务队列线程,获取数据--------

System.out.println("===QueueThread starting ... ... ");

queueThread = new Thread(new QueueThread(),"QueueThread");

queueThread.start();

System.out.println("===Initial thread Pool ... ...");

//初始化线程池的最小线程数,并放入池中

for(int i=0; i

run = new WorkThread();

workThread = new Thread(run,"WorkThread_"+System.currentTimeMillis()+i);

workThread.start();

if(i == threads_min_num -1){

workThread = null;

run = null;

}

}

System.out.println("===Initial thread Pool..........over ,and get pool's size:"+pool.size());

//线程池线程动态增加线程算法--------------

while(!close){

//等待5秒钟,等上述线程都启动----------

synchronized(this){

try{

System.out.println("===Wait the [last time] threads starting ....");

this.wait(15000);

}

catch(Throwable ex){

System.out.println("###PoolWatchDog invoking is failure ... "+ex);

}

}//end synchronized

//开始增加线程-----------------------spread动作

int queue_size = queue.getTaskSize();

int temp_size = (queue_size - threads_min_num);

if((temp_size > 0) && (temp_size/threads_increase_step > 2) ){

System.out.println("================Spread thread pool starting ....");

for(int i=0; i

System.out.println("=== Spread thread num : "+i);

run = new WorkThread();

workThread = new Thread(run,"WorkThread_"+System.currentTimeMillis()+i);

workThread.start();

}//end for

workThread = null;

run = null;

System.out.println("===Spread thread pool is over .... and pool size:"+pool.size());

}//end if

//删除已经多余的睡眠线程-------------shrink动作

int more_sleep_size = pool.size() - threads_min_num;//最多能删除的线程数

int sleep_threads_size = passivePool.size();

if(more_sleep_size >0 && sleep_threads_size >0){

System.out.println("================Shrink thread pool starting ....");

for(int i=0; i < more_sleep_size && i < sleep_threads_size ; i++){

System.out.println("=== Shrink thread num : "+i);

Thread removeThread = (Thread)passivePool.get(0);

if(removeThread != null && removeThread.isAlive() && !removeThread.isInterrupted()){

removeThread.interrupt();

}

}

System.out.println("===Shrink thread pool is over .... and pool size:"+pool.size());

}

System.out.println("===End one return [shrink - spread operator] ....");

}//end while

}//end run

}//end private class

/**

* 工作线程

*/

class WorkThread implements Runnable{

public WorkThread(){

}

public void run(){

String name = Thread.currentThread().getName();

System.out.println("===Thread.currentThread():"+name);

pool.add(Thread.currentThread());

while(true){

//获取任务---------

ITask task = null;

try{

System.out.println("===Get task from queue is starting ... ");

//看线程是否被中断,如果被中断停止执行任务----

if(Thread.currentThread().isInterrupted()){

System.out.println("===Breaking current thread and jump whlie [1] ... ");

break;

}

task = queue.getTask();

}

catch(Throwable ex){

System.out.println("###No task in queue:"+ex);

}//end tryc

if(task != null){

//执行任务---------

try{

System.out.println("===Execute the task is starting ... ");

//看线程是否被中断,如果被中断停止执行任务----

if(Thread.currentThread().isInterrupted()){

System.out.println("===Breaking current thread and jump whlie [1] ... ");

break;

}

task.executeTask();

//任务执行完毕-------

System.out.println("===Execute the task is over ... ");

}

catch(Throwable ex){

System.out.println("###Execute the task is failure ... "+ex);

}//end tryc

}else{

//没有任务,则钝化线程至规定时间--------

synchronized(this){

try{

System.out.println("===Passivate into passivePool ... ");

//看线程是否被中断,如果被中断停止执行任务----

boolean isInterrupted = Thread.currentThread().isInterrupted();

if(isInterrupted){

System.out.println("===Breaking current thread and jump whlie [1] ... ");

break;

}

// passivePool.add(this);

passivePool.add(Thread.currentThread());

//准备睡眠线程-------

isInterrupted = Thread.currentThread().isInterrupted();

if(isInterrupted){

System.out.println("===Breaking current thread and jump whlie [2] ... ");

break;

}

this.wait(IDLE_TIMEOUT);

}

catch(Throwable ex1){

System.out.println("###Current Thread passivate is failure ... break while cycle. "+ex1);

break;

}

}

}

}//end while--------

if(pool.contains(passivePool)){

pool.remove(this);

}

if(passivePool.contains(passivePool)){

passivePool.remove(this);

}

System.out.println("===The thread execute over ... ");

}//end run----------

}

class QueueThread implements Runnable{

public QueueThread(){

}

public void run(){

while(true){

//自动装在任务--------

queue.autoAddTask();

System.out.println("===The size of queue's task is "+queue.getTaskSize());

synchronized(this){

if(Thread.currentThread().isInterrupted()){

break;

}else{

try{

this.wait(queue.getLoadDataPollingTime());

}

catch(Throwable ex){

System.out.println("===QueueThread invoked wait is failure ... break while cycle."+ex);

break;

}

}//end if

}//end synchr

}//end while

}//end run

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值