java - 如何在线程池中编写run方法

标签 java multithreading threadpool

阅读《线程池》后我感到非常困惑。我了解了这个概念,以及它们实际上是如何工作的。 但我对如何编码这部分感到困惑。

网上查了很多资料。最后我得到了一个博客,其中有代码,如下所示,

条件是不要使用内置类

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

帮助我。

提前致谢。

最佳答案

  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.

如果任务抛出异常,则不执行任何特殊操作,只是不要传递异常。

      }
    }
  }

关于java - 如何在线程池中编写run方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14459454/

相关文章:

java - 您可以分配唯一的线程 ID 并从外部程序访问线程吗?

java - 如何在创建时将域添加到 Google Cloud Storage 对象 ACL?

java - 求插入排序的大o时间复杂度

c# - 如何检测线程是否具有 Windows 句柄?

java - 在同一个锁上同步

java以伪 'fixed'速率安排可调用(例如钟形曲线分布)

java - 如何使用 Lambda 表达式获取单词的平均长度

java - 修改数据库版本后出错

c++ - Boost::ASIO:在多线程上使用 tcp::socket

python - 当QThreadPool不为空时退出?