线程池简单代码实现
来源:互联网 发布:linux 内核 书籍 编辑:程序博客网 时间:2024/04/30 01:12
public class ThreadPool {
private List<Thread> listThread = new ArrayList<Thread>();
private List<Task> listRunnable = new ArrayList();
private int size;
public ThreadPool(int size){
this.size = size;
for(int i = 0; i < size; i++){
MyThread th = new MyThread(listRunnable, i+1);
th.start();
}
}
public void excuteRun(Task runnable){
synchronized(listRunnable){
listRunnable.add(runnable);
listRunnable.notifyAll();
}
}
}
class MyThread extends Thread{
private List<Task> listRunnable;
private int i;
public MyThread(List<Task> listRunnable,int i){
this.listRunnable = listRunnable;
this.i = i;
}
public void run(){
while(true){
Task runnable = null;
synchronized (this.listRunnable) {
if(this.listRunnable.size() > 0){
runnable = this.listRunnable.remove(0);
}else{
try {
this.listRunnable.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(runnable != null){
runnable.setJ(i);
runnable.run();
}
}
}
}
class Task implements Runnable{
private int k ;
private int j;
public Task(int i){
k = i;
}
@Override
public void run() {
System.out.println("第" + this.getJ() +"个线程,执行的" + k +"个任务");
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
}
- 线程池简单代码实现
- 简单线程池的代码实现
- java 简单线程池的实现代码
- 200行C代码实现简单线程池【Linux】
- 200行C代码实现简单线程池
- 简单线程池实现
- 线程池简单实现
- 线程池(代码简单分析)
- 简单的线程池实现
- 线程池的简单实现
- 简单线程池的实现
- Python线程池简单实现
- 线程池的简单实现
- 简单的线程池实现
- 简单线程池的实现
- 线程池的简单实现
- C++实现简单线程池
- 简单线程池实现+测试
- 指蝗毙腋卸干干刈系荣猜鼐巢掠旨
- Ubuntu安装scala
- 狂抓
- 椰壳活性炭的作用是什么哪种活性炭的效果好果壳活性炭是用什么材质做的生产厂家分布市场价格净水黄金提取专用活性炭
- 命令行
- 线程池简单代码实现
- 编译或打包程序时eclipse报Unable to execute dex: GC overhead limit exceeded或java heap space .
- 只有这个手势适合内的
- 证明书怎么写
- 浅析HTML5在移动应用开发中的使用
- 生成URL编码请求字符串和解析URL编码请求字符串
- 深入了解OpenGL——纹理基础
- 6.26
- C#中@符号的用法大全