- 浏览: 6644 次
- 性别:
- 来自: 上海
最近访客 更多访客>>
文章分类
最新评论
-
helloguoxing:
//测试类的代码,我稍微改装了下。
package com ...
线程池的实现例子1 -
helloguoxing:
请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有 ...
线程池的实现例子1 -
helloguoxing:
请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有 ...
线程池的实现例子1 -
helloguoxing:
楼主这个线程池写的不错,我运行了下,优点略懂。
线程池的实现例子1
import java.util.LinkedList;
import java.util.List;
public class ThreadPool ...{
private static ThreadPool instance = null;
// 优先级低
public static final int PRIORITY_LOW = 0;
// 普通
public static final int PRIORITY_NORMAL = 1;
// 高
public static final int PRIORITY_HIGH = 2;
// 用以保存空闲连接
private List[] _idxThreads;
// 关闭
private boolean _shutdown = false;
// 线程数量
private int _threadCount = 0;
// debug信息是否输出
private boolean _debug = false;
/** *//**
* 返回ThreadPool实例
*
* @return
*/
public static ThreadPool getInstance() ...{
if (instance == null) ...{
instance = new ThreadPool();
}
return instance;
}
// 初始化线程list
private ThreadPool() ...{
this._idxThreads = new List[] ...{ new LinkedList(), new LinkedList(),
new LinkedList() };
this._threadCount=0;
}
/** *//**
* 同步方法,完成任务后将资源放回线程池中
* @param repool
*/
protected synchronized void repool(Pooled repool) ...{
if (this._shutdown) ...{
if (this._debug) ...{
System.out.println("ThreadPool.repool():重设中……");
}
// 优先级别判定
switch (repool.getPriority()) ...{
case Thread.MIN_PRIORITY:
this._idxThreads[PRIORITY_LOW].add(repool);
break;
case Thread.NORM_PRIORITY:
this._idxThreads[PRIORITY_NORMAL].add(repool);
break;
case Thread.MAX_PRIORITY:
this._idxThreads[PRIORITY_HIGH].add(repool);
break;
default:
throw new IllegalStateException("没有此种级别");
}
// 通知所有线程
notifyAll();
} else ...{
if (this._debug) ...{
System.out.println("ThreadPool.repool():注销中……");
}
repool.shutDown();
}
if(this._debug)...{
System.out.println("ThreadPool.repool():完成");
}
}
public void setDebug(boolean debug)...{
this._debug=debug;
}
public synchronized void shutDown()...{
this._shutdown=true;
if(this._debug)...{
System.out.println("ThreadPool.shutDown():关闭中……");
}
for(int index=0;index<=PRIORITY_NORMAL;index++)...{
List threads=this._idxThreads[index];
for(int threadIndex=0;threadIndex<threads.size();threadIndex++)...{
Pooled idleThread=(Pooled)threads.get(threadIndex);
idleThread.shutDown();
}
}
notifyAll();
}
/** *//**
* 以指定的优先级启动线程
* @param target
* @param priority
*/
public synchronized void start(Runnable target,int priority)...{
Pooled thread=null;
List idleList=this._idxThreads[priority];
int idleSize=idleList.size();
if(idleSize>0)...{
int lastIndex=idleSize-1;
thread=(Pooled)idleList.get(lastIndex);
idleList.remove(idleList);
thread.setTarget(target);
}else...{
this._threadCount++;
thread=new Pooled(target,"Pooled->"+this._threadCount,this);
switch(priority)...{
case PRIORITY_LOW:
thread.setPriority(Thread.MIN_PRIORITY);
break;
case PRIORITY_NORMAL:
thread.setPriority(Thread.NORM_PRIORITY);
break;
case PRIORITY_HIGH:
thread.setPriority(Thread.MAX_PRIORITY);
break;
default:
thread.setPriority(Thread.NORM_PRIORITY);
}
//启动
thread.start();
}
}
/** *//**
* 返回线程数量
*
* @return
*/
public int getThreadsCount() ...{
return this._threadCount;
}
}
Pooled.java:
package org.loon.framework.util.test;
public class Pooled extends Thread ...{
private ThreadPool _pool;
private Runnable _target;
private boolean _shutdown = false;
private boolean _idle = false;
public Pooled(Runnable target) ...{
super(target);
}
public Pooled(Runnable target, String name) ...{
super(target, name);
}
public Pooled(Runnable target, String name, ThreadPool pool) ...{
super(name);
this._pool = pool;
this._target = target;
}
public Pooled(String name) ...{
super(name);
}
public Pooled(ThreadGroup group, Runnable target) ...{
super(group, target);
}
public Pooled(ThreadGroup group, Runnable target, String name) ...{
super(group, target, name);
}
public Pooled(ThreadGroup group, String name) ...{
super(group, name);
}
public Runnable getTarget() ...{
return this._target;
}
public boolean isIdle() ...{
return this._idle;
}
public void run() ...{
while (!this._shutdown) ...{
this._idle = false;
if (this._target != null) ...{
this._target.run();
}
this._idle = true;
try ...{
this._pool.repool(this);
synchronized (this) ...{
wait();
}
} catch (InterruptedException ex) ...{
System.err.println(ex.getMessage());
}
this._idle = false;
}
}
public synchronized void setTarget(Runnable target) ...{
this._target = target;
notifyAll();
}
public synchronized void shutDown() ...{
this._shutdown = true;
notifyAll();
}
}
测试用类:
public class ThreadPoolTest ...{
private static Runnable createRunnable(final int id) ...{
return new Runnable() ...{
public void run() ...{
System.out.println("线程" + id + ",运行 ");
try ...{
Thread.sleep(1000);
}
catch (InterruptedException ex) ...{ }
System.out.println("线程" + id + ",结束");
}
};
}
public static void main(String[]args)...{
ThreadPool pool=ThreadPool.getInstance();
pool.setDebug(true);
for (int i=1; i<=10; i++) ...{
//根据数值,设定不同优先级
if(i%2==0)...{
pool.start(createRunnable(i), ThreadPool.PRIORITY_HIGH);
}else...{
pool.start(createRunnable(i), ThreadPool.PRIORITY_LOW);
}
}
System.out.println("线程池测试中……");
System.out.println("线程池线程总数:"+pool.getThreadsCount());
pool.shutDown();
}
}
import java.util.List;
public class ThreadPool ...{
private static ThreadPool instance = null;
// 优先级低
public static final int PRIORITY_LOW = 0;
// 普通
public static final int PRIORITY_NORMAL = 1;
// 高
public static final int PRIORITY_HIGH = 2;
// 用以保存空闲连接
private List[] _idxThreads;
// 关闭
private boolean _shutdown = false;
// 线程数量
private int _threadCount = 0;
// debug信息是否输出
private boolean _debug = false;
/** *//**
* 返回ThreadPool实例
*
* @return
*/
public static ThreadPool getInstance() ...{
if (instance == null) ...{
instance = new ThreadPool();
}
return instance;
}
// 初始化线程list
private ThreadPool() ...{
this._idxThreads = new List[] ...{ new LinkedList(), new LinkedList(),
new LinkedList() };
this._threadCount=0;
}
/** *//**
* 同步方法,完成任务后将资源放回线程池中
* @param repool
*/
protected synchronized void repool(Pooled repool) ...{
if (this._shutdown) ...{
if (this._debug) ...{
System.out.println("ThreadPool.repool():重设中……");
}
// 优先级别判定
switch (repool.getPriority()) ...{
case Thread.MIN_PRIORITY:
this._idxThreads[PRIORITY_LOW].add(repool);
break;
case Thread.NORM_PRIORITY:
this._idxThreads[PRIORITY_NORMAL].add(repool);
break;
case Thread.MAX_PRIORITY:
this._idxThreads[PRIORITY_HIGH].add(repool);
break;
default:
throw new IllegalStateException("没有此种级别");
}
// 通知所有线程
notifyAll();
} else ...{
if (this._debug) ...{
System.out.println("ThreadPool.repool():注销中……");
}
repool.shutDown();
}
if(this._debug)...{
System.out.println("ThreadPool.repool():完成");
}
}
public void setDebug(boolean debug)...{
this._debug=debug;
}
public synchronized void shutDown()...{
this._shutdown=true;
if(this._debug)...{
System.out.println("ThreadPool.shutDown():关闭中……");
}
for(int index=0;index<=PRIORITY_NORMAL;index++)...{
List threads=this._idxThreads[index];
for(int threadIndex=0;threadIndex<threads.size();threadIndex++)...{
Pooled idleThread=(Pooled)threads.get(threadIndex);
idleThread.shutDown();
}
}
notifyAll();
}
/** *//**
* 以指定的优先级启动线程
* @param target
* @param priority
*/
public synchronized void start(Runnable target,int priority)...{
Pooled thread=null;
List idleList=this._idxThreads[priority];
int idleSize=idleList.size();
if(idleSize>0)...{
int lastIndex=idleSize-1;
thread=(Pooled)idleList.get(lastIndex);
idleList.remove(idleList);
thread.setTarget(target);
}else...{
this._threadCount++;
thread=new Pooled(target,"Pooled->"+this._threadCount,this);
switch(priority)...{
case PRIORITY_LOW:
thread.setPriority(Thread.MIN_PRIORITY);
break;
case PRIORITY_NORMAL:
thread.setPriority(Thread.NORM_PRIORITY);
break;
case PRIORITY_HIGH:
thread.setPriority(Thread.MAX_PRIORITY);
break;
default:
thread.setPriority(Thread.NORM_PRIORITY);
}
//启动
thread.start();
}
}
/** *//**
* 返回线程数量
*
* @return
*/
public int getThreadsCount() ...{
return this._threadCount;
}
}
Pooled.java:
package org.loon.framework.util.test;
public class Pooled extends Thread ...{
private ThreadPool _pool;
private Runnable _target;
private boolean _shutdown = false;
private boolean _idle = false;
public Pooled(Runnable target) ...{
super(target);
}
public Pooled(Runnable target, String name) ...{
super(target, name);
}
public Pooled(Runnable target, String name, ThreadPool pool) ...{
super(name);
this._pool = pool;
this._target = target;
}
public Pooled(String name) ...{
super(name);
}
public Pooled(ThreadGroup group, Runnable target) ...{
super(group, target);
}
public Pooled(ThreadGroup group, Runnable target, String name) ...{
super(group, target, name);
}
public Pooled(ThreadGroup group, String name) ...{
super(group, name);
}
public Runnable getTarget() ...{
return this._target;
}
public boolean isIdle() ...{
return this._idle;
}
public void run() ...{
while (!this._shutdown) ...{
this._idle = false;
if (this._target != null) ...{
this._target.run();
}
this._idle = true;
try ...{
this._pool.repool(this);
synchronized (this) ...{
wait();
}
} catch (InterruptedException ex) ...{
System.err.println(ex.getMessage());
}
this._idle = false;
}
}
public synchronized void setTarget(Runnable target) ...{
this._target = target;
notifyAll();
}
public synchronized void shutDown() ...{
this._shutdown = true;
notifyAll();
}
}
测试用类:
public class ThreadPoolTest ...{
private static Runnable createRunnable(final int id) ...{
return new Runnable() ...{
public void run() ...{
System.out.println("线程" + id + ",运行 ");
try ...{
Thread.sleep(1000);
}
catch (InterruptedException ex) ...{ }
System.out.println("线程" + id + ",结束");
}
};
}
public static void main(String[]args)...{
ThreadPool pool=ThreadPool.getInstance();
pool.setDebug(true);
for (int i=1; i<=10; i++) ...{
//根据数值,设定不同优先级
if(i%2==0)...{
pool.start(createRunnable(i), ThreadPool.PRIORITY_HIGH);
}else...{
pool.start(createRunnable(i), ThreadPool.PRIORITY_LOW);
}
}
System.out.println("线程池测试中……");
System.out.println("线程池线程总数:"+pool.getThreadsCount());
pool.shutDown();
}
}
评论
4 楼
helloguoxing
2010-08-12
//测试类的代码,我稍微改装了下。
package com.soufang.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ThreadPoolTest { private static Runnable createRunnable(final int id){ return new Runnable(){ public void run(){ System.out.println("线程"+id+",运行"); try{ Thread.sleep(1000); }catch(InterruptedException e){ System.out.println("线程"+id+",结束"); } } }; } public static void testThread(){ ThreadPool pool=ThreadPool.getInstance(); pool.setDebug(true); for(int i=1;i<=10;i++){ //根据数值,设定不同优先级 if(i%2==0){ pool.start(createRunnable(i), ThreadPool.PRIORITY_HIGH); }else{ pool.start(createRunnable(i), ThreadPool.PRIORITY_LOW); } } //System.out.println("线程池线程总数:"+pool.getThreadsCount()); pool.shutDown(); } public static String read(){ String read=""; BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in)); try { boolean bl=true; while(bl){ read=buffer.readLine(); //关闭连接 if(read.contains(";")){ bl=false; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return read; } public void toto(){ ThreadPool pool=ThreadPool.getInstance(); String count=read(); if(""!=count && !"".equals(count)){ if(count.contains("1")){ testThread(); toto(); }else if(count.contains("2")){ pool.shutDown(); toto(); }else if(count.contains("3")){ System.out.println("线程池线程总数:"+pool.getThreadsCount()); toto(); }else if(count.contains("4")){ System.out.println("线程池测试中......"); toto(); }else if(count.contains("5")){ System.out.println("欢迎使用!"); }else{ System.out.println("请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有线程数量 4、线程池当前状态 5、退出程序"); toto(); } } } public static void main(String[] args){ ThreadPoolTest threadtest=new ThreadPoolTest(); System.out.println("请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有线程数量 4、线程池当前状态 5、退出程序"); threadtest.toto(); } }
3 楼
helloguoxing
2010-08-12
请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有线程数量 4、线程池当前状态 5、退出程序
1
1;
线程2,运行
线程4,运行
线程6,运行
线程1,运行
线程8,运行
ThreadPool.shutDown();关闭中。。。。。。
线程10,运行
线程7,运行
线程5,运行
线程3,运行
线程9,运行
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
2;
ThreadPool.shutDown();关闭中。。。。。。
3;
线程池线程总数:10
4;
线程池测试中......
5;
欢迎使用!
5;
5;
//唯一的问题,就是输入5的时候不能退出来。不知道楼主有没有发现。
1
1;
线程2,运行
线程4,运行
线程6,运行
线程1,运行
线程8,运行
ThreadPool.shutDown();关闭中。。。。。。
线程10,运行
线程7,运行
线程5,运行
线程3,运行
线程9,运行
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
2;
ThreadPool.shutDown();关闭中。。。。。。
3;
线程池线程总数:10
4;
线程池测试中......
5;
欢迎使用!
5;
5;
//唯一的问题,就是输入5的时候不能退出来。不知道楼主有没有发现。
2 楼
helloguoxing
2010-08-12
请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有线程数量
1
请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有线程数量
线程2,运行
线程4,运行
线程6,运行
线程8,运行
线程池测试中......
线程10,运行
线程池线程总数:10
ThreadPool.shutDown();关闭中。。。。。。
ThreadPool.shutDown();关闭中。。。。。。
线程1,运行
线程3,运行
线程5,运行
线程7,运行
线程9,运行
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
1
请重新输入:1、启动线程 2、关闭线程 3、查询当前线程池所有线程数量
线程2,运行
线程4,运行
线程6,运行
线程8,运行
线程池测试中......
线程10,运行
线程池线程总数:10
ThreadPool.shutDown();关闭中。。。。。。
ThreadPool.shutDown();关闭中。。。。。。
线程1,运行
线程3,运行
线程5,运行
线程7,运行
线程9,运行
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
ThreadPool.repool():重设中。。。。。。
ThreadPool.repool();完成
1 楼
helloguoxing
2010-08-12
楼主这个线程池写的不错,我运行了下,优点略懂。
发表评论
-
JAVA的小圈套
2008-07-11 10:13 619public class Test { static ... -
接口的的作用(注意第13行)
2008-07-09 09:45 381interface B { String getS() ... -
用DOM读取XML文件的案例
2008-07-09 09:16 821//用DOM读取XML public class A { ... -
关于实现简单线程池的改写
2008-07-08 10:30 714import java.util.*; import jav ... -
java写的简单线程池
2008-07-08 10:26 950import java.util.*; import java ... -
线程池的实现例子2
2008-07-08 10:25 938** 线程池是一组线程,限制执行任务的线程数 */ p ... -
有关于Arrays的一些尝试
2008-07-08 10:20 591import java.util.Arrays; import ... -
规范的流方法之复制文件代码
2008-07-08 09:58 748import java.util.ArrayList; imp ...
相关推荐
C++线程池实现 vc2015编译
VC实现的线程池例程,深入地讲解操作系统实现的线程池的优越性,尝试实现一个简单的线程池模型。
自已用Qt写的线程池实现程序,可以正常运行,用socket进行通讯,很适合初学者学习借鉴;
vc实现多线程池的例子threadpooldemosrc.rar
企业级跨平台C++线程池实现,带vs工程例子,易于使用,功能完善,经过企业级的线上测试的C++线程池实现。
java 线程池 实现 例子,线程池实例
Quartz 线程池 中剥离出来的代码 不例子
c++线程池类以及线程类代码实现,对于初学者来说是个很好的例子。
线程池实现的例子,不错,有用的可以参看
在什么情况下使用线程池? 1.单个任务处理的时间比较短 2.将需处理的任务的数量大 使用线程池的好处: 1.减少在创建和销毁线程上所花的时间以及系统资源的开销 2....下面是一个线程池例子,经测试绝对可行:
VC写的一个线程池的管理实例,实现多线程管理的好代码值得学习
因为项目需要,还涉及到排队下载的功能,所以就选择了线程池来管理线程以及线程池里面的任务队列workQueue来实现项目所需的功能; b:在实际使用中,服务器在创建和销毁线程上花费的时间和消耗的系统资源都相当大...
As you see ,在C++类中创建线程池是有限制的,为了使已经写好的代码维持最小改动,我将网上广为流传的线程池代码(至今没有找到一个可以顺利运行并直接使用的例子)进行了加工,使其满足我们项目的要求。...
c++实现的线程池,含有类图、流程图、简单的使用例子、makefile。
C++封装的linux下的可动态增减的线程池,总共有三个源文件: main.cpp 为例子程序,ThreadPool.cpp和ThreadPool.h为线程池实现文件,仅供学习交流使用
用C#自己实现的线程池。很好的范例。有助于对线程池,线程同步,线程锁的学习。很真研究通这个例子之后,我相信你对线程肯定有深刻的了解
1.通过一个主线程来监视是否有新任务,如果有新任务,则判断当前的线程数是否大于最大线程数,如果小于最大线程数则创建新线程,反之则加入线程队列等待执行。 2.当时间经过过长(1 分钟,可手动设置)主线程会自动...
在C#中可以通过System.Threading.ThreadPool类来实现,在默认情况下,ThreadPool最大可建立500个工作线程和1000个I/O线程(根据机器CPU个数和.net framework版本的不同,这些数据可能会有变化) 下载文件服务器的...
线程池示例(包含自定义拒绝策略),演示了如何创建一个线程池,以及添加到队列的过程,先添加到工作线程,然后是缓存队列,最后是创建临时线程
主要介绍了php与python实现的线程池多线程爬虫功能,结合实例形式分析了php与python实现线程池多线程爬虫的完整实现方法,需要的朋友可以参考下