2013-01-22 42 views
0

通过阅读线程池,我非常困惑。我学到了这个概念,他们是如何实际工作的。 但是我在这个部分感到困惑,怎么编码呢。如何在线程池中运行代码的方法

我在网上搜了很多。最后,我得到了一个博客,有代码,如下,

条件是不使用内置类

代码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?? 

帮帮我。

在此先感谢。

+1

为什么你必须编写自己的线程池?最容易使用执行者。* ThreadPool()方法创建一个线程池,然后使用提交(*)方法来提交Callable或Runnable。 – allprog

+0

@allprog:我创建了该代码,该代码很容易构建。但是,我的老师要求在不使用内置课程的情况下进行构建。这就是为什么我发布这个。 – devsda

+0

不错,你让社区检查作业。 :)不幸的是,执行ExecutorService的权利很难。如果您提供取消异步Future接口,那么您必须非常小心。您应该查看JDK源代码以获得正确的实现。这将比现在复杂得多。 – allprog

回答

2
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. 

如果任务抛出异常,只是不要传递它什么都不做。

 } 
    } 
    } 
+0

'public PoolThread(BlockingQueue queue){ taskQueue = queue; }' 据我所知,这些行的作品是给所有线程工作者提供taskqueue的副本。我对吗 ? – devsda

+0

告诉我更多的事情,我们如何给**代码1 **给予inut数组。 因为要开始我们必须调用代码1的执行方法。 – devsda

+0

@FreakyCheeky:第一个问题:是的。每个线程获取对同一队列的引用。第二个问题:您调用execute函数并将任务放到共享队列中。 –

1

编辑:

我现在明白了,这是一类项目,但我会离开我为后人的答案。

如果你想Java中使用线程池,然后这一切已经为您在java.util.concurrent.*类已经实现。其他答案解决和解释你的具体代码。

例如,您需要使用ExecutorService代码设置线程池。在封面下面,ExecutorService处理线程并使用LinkedBlockingQueue。您定义了实现'Runnable'的类MyJob,并执行池中线程运行的工作。根据您的需要,它可以是短期或长期运行的任务。

// create a thread pool with 10 workers 
ExecutorService threadPool = Executors.newFixedThreadPool(10); 
// or you can create an open-ended thread pool 
// ExecutorService threadPool = Executors.newCachedThreadPool(); 
// define your jobs somehow 
for (MyJob job : jobsToDo) { 
    threadPool.submit(job); 
} 
// once we have submitted all jobs to the thread pool, it should be shutdown 
threadPool.shutdown(); 
... 
public class MyJob implements Runnable { 
    // you can construct your jobs and pass in context for them if necessary 
    public MyJob(String someContext) { 
     ... 
    } 
    public void run() { 
     // process the job 
    } 
} 
+0

其实我是通过使用内置类来创建这段代码的,但是我的老师跟我说要自己做这个,不用内置类 – devsda

相关问题