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

助けて。

前もって感謝します。

4

2 に答える 2

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.

タスクが例外をスローした場合は特別なことは何もしないでください。それを渡さないでください。

      }
    }
  }
于 2013-01-22T13:20:36.427 に答える
1

編集:

これはクラスプロジェクトであることがわかりましたが、後世のために答えを残しておきます。

Javaでスレッドプールを使用しようとしている場合は、これらすべてがjava.util.concurrent.*クラスによってすでに実装されています。他の回答はあなたの特定のコードに対処し、説明します。

たとえば、これはExecutorServiceコードを使用してスレッドプールを設定するために必要なものです。カバーの下でExecutorServiceスレッドを処理し、を使用しLinkedBlockingQueueます。MyJob'Runnable'を実装し、プール内のスレッドによって実行される作業を実行するクラスを定義します。必要なものに応じて、短時間または長時間実行されるタスクになります。

// 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
    }
}
于 2013-01-22T13:20:09.630 に答える