如何在线程池中编写运行方法的代码 [英] How to code run method in Thread pooling

查看:85
本文介绍了如何在线程池中编写运行方法的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过阅读线程池,我感到非常困惑.我了解了这个概念,以及它们实际上是如何工作的. 但是我对如何编写代码感到困惑.

I got very confused by reading Thread Pooling. I learnt the concept, how they actually works. But I confused in the part , how to code this.

我在网上搜索了很多东西.最后,我得到了一个博客,其中包含代码,如下所示

I searched a lot on the net. Finally I got a blog, that have codes , given below,

条件是,不要使用内置类

代码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);
  }    
}

我试图理解这段代码的作用. 但是我没有得到这段代码的流程.您能帮我理解这段代码吗?

I tried to understand , what this code do. But I dont get the flow of this code. Can you help me to understand this code.

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();

将头任务从任务队列中拉出.

Pull the head task off the task queue.

        runnable.run();

运行任务.

      } catch(Exception e){
        //log or otherwise report exception,
        //but keep pool thread alive.

如果任务抛出异常,请不要做任何特殊的事情,只是不要将其传递出去.

Do nothing special if the task throws an exception, just don't pass it on.

      }
    }
  }

这篇关于如何在线程池中编写运行方法的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆