KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > ThreadPool


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import java.util.*;
7 import java.util.logging.*;
8 import net.nutch.util.LogFormatter;
9
10 /************************************************
11  * ThreadPool.java
12  *
13  * ThreadPool maintains a large set of threads, which
14  * can be dedicated to a certain task, and then recycled.
15  ***********************************************/

16 public class ThreadPool {
17     /**
18      * A TaskThread sits in a loop, asking the pool
19      * for a job, and servicing it. That's all it does.
20      */

21     class TaskThread extends Thread JavaDoc {
22         /**
23          * Get a job from the pool, run it, repeat.
24          * If the job is null, we exit the loop.
25          */

26         public void run() {
27             while (true) {
28                 Runnable JavaDoc r = obtainJob();
29                 if (r == null) {
30                     break;
31                 }
32                 try {
33                     r.run();
34                 } catch (Exception JavaDoc e) {
35                     System.err.println("E: " + e);
36                     e.printStackTrace();
37                 }
38             }
39         }
40     }
41
42     int numThreads;
43     boolean running = false;
44     Vector jobs;
45
46     /**
47      * Creates a pool of numThreads size.
48      * These threads sit around waiting for jobs to
49      * be posted to the list.
50      */

51     public ThreadPool(int numThreads) {
52         this.numThreads = numThreads;
53         jobs = new Vector(37);
54         running = true;
55
56         for (int i = 0; i < numThreads; i++) {
57             TaskThread t = new TaskThread();
58             t.start();
59         }
60         Logger l = LogFormatter.getLogger("net.nutch.util");
61         l.fine("ThreadPool created with " + numThreads + " threads.");
62     }
63
64     /**
65      * Gets a job from the queue, returns to worker thread.
66      * When the pool is closed down, return null for all
67      * obtainJob() requests. That tells the thread to
68      * shut down.
69      */

70     Runnable JavaDoc obtainJob() {
71         Runnable JavaDoc job = null;
72
73         synchronized (jobs) {
74             while (job == null && running) {
75                 try {
76                     if (jobs.size() == 0) {
77                         jobs.wait();
78                     }
79                 } catch (InterruptedException JavaDoc ie) {
80                 }
81
82                 if (jobs.size() > 0) {
83                     job = (Runnable JavaDoc) jobs.firstElement();
84                     jobs.removeElementAt(0);
85                 }
86             }
87         }
88
89         if (running) {
90             // Got a job from the queue
91
return job;
92         } else {
93             // Shut down the pool
94
return null;
95         }
96     }
97
98     /**
99      * Post a Runnable to the queue. This will be
100      * picked up by an active thread.
101      */

102     public void addJob(Runnable JavaDoc runnable) {
103         synchronized (jobs) {
104             jobs.add(runnable);
105             jobs.notifyAll();
106         }
107     }
108
109     /**
110      * Turn off the pool. Every thread, when finished with
111      * its current work, will realize that the pool is no
112      * longer running, and will exit.
113      */

114     public void shutdown() {
115         running = false;
116         Logger l = LogFormatter.getLogger("net.nutch.util");
117         l.fine("ThreadPool shutting down.");
118     }
119 }
120
Popular Tags