【发布时间】:2013-01-22 13:16:24
【问题描述】:
阅读线程池,我感到非常困惑。我了解了这个概念,它们实际上是如何工作的。 但我在这部分感到困惑,如何编码。
我在网上搜索了很多。最后我得到了一个博客,里面有代码,如下所示,
条件是,不要使用内置类
代码 1
public class ThreadPool {
private BlockingQueue taskQueue = null;
private List<PoolThread> threads = new ArrayList<PoolThread>();
private boolean isStopped = false;
public ThreadPool(int noOfThreads, int maxNoOfTasks){
taskQueue = new BlockingQueue(maxNoOfTasks);
for(int i=0; i<noOfThreads; i++){
threads.add(new PoolThread(taskQueue));
}
for(PoolThread thread : threads){
thread.start();
}
}
public void synchronized execute(Runnable task){
if(this.isStopped) throw
new IllegalStateException("ThreadPool is stopped");
this.taskQueue.enqueue(task);
}
public synchronized void stop(){
this.isStopped = true;
for(PoolThread thread : threads){
thread.stop();
}
}
}
代码 2
public class PoolThread extends Thread {
private BlockingQueue taskQueue = null;
private boolean isStopped = false;
public PoolThread(BlockingQueue queue){
taskQueue = queue;
}
public void run(){
while(!isStopped()){
try{
Runnable runnable = (Runnable) taskQueue.dequeue();
runnable.run();
} catch(Exception e){
//log or otherwise report exception,
//but keep pool thread alive.
}
}
}
public synchronized void stop(){
isStopped = true;
this.interrupt(); //break pool thread out of dequeue() call.
}
public synchronized void isStopped(){
return isStopped;
}
}
代码 3:-
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
我试图理解这段代码的作用。 但我不明白这段代码的流程。你能帮我理解这段代码吗?
Mainly I have problems in **Code 2 :- run method**
Why execute method's argument are of Runnable type?
How input array given to this code??
帮帮我。
提前致谢。
【问题讨论】:
-
为什么一定要自己写线程池?最容易使用 Executors.*ThreadPool() 方法来创建线程池,然后在其上使用 submit(*) 方法来提交 Callable 或 Runnable。
-
@allprog:我编写了那个代码,这个代码很容易构建。但是我的老师要求在不使用内置类的情况下构建它。这就是我发布这个的原因。
-
很好,你让社区检查作业。 :) 不幸的是,正确地执行 ExecutorService 很难。如果您为异步 Future 接口提供取消功能,那么您必须非常小心。您应该查看 JDK 源代码以获得正确的实现。它会比你现在拥有的复杂得多。
-
@allprog:不,我不是让社区来检查代码。我花了很多时间,最后我的小模块有问题,然后才问这个答案。我会照顾你的建议。谢谢:)
-
放轻松。问总比不知道答案好。 :) 您也应该对 BlockingQueue 使用泛型,但在任何情况下都不要将 Object 类型用于入队和出队。它会破坏你的代码!这个功能在JDK中有一个同名的实现,你不能用吗?我认为 ExecutorService (正确实施)已经比简单的家庭作业或作业要复杂得多。
标签: java multithreading threadpool