Re: 线程池的实现

[url=http://www.iteye.com/topic/104432]LZ[/url]的思路,是线程共享任务队列,线程按同步方法获取一个非空的任务执行:工作线程自己去“拉单”干。

我提供另外一种思路,线程是被动,由别人(监工)分配,激发:

有一个主线程--“监工”:负责查看任务队列中是否有任务,如果有,取出一个任务,设置到一个“空闲”的线程中,并notify该线程。

[code]
while (hasTask) {
1、get the task from task quene
2、get an waiting worker thread, or create a new workder thread
3、set the task to the thread, and set the thread status running
synchronized(thread) {
4、thread.nofity();//not notifyAll()
}
}

[/code]


线程类的run方法会是这样的:
[code]
synchronized(this) {
while (!shutdown) {
if (status == running) {
1、run the task;
2、task = null;
3、status = waiting;
4、manager.done(this, task);//告知监工自己已经完成这个任务
}
5、this.wait();
}
}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值