1 2 3 4 package net.nutch.util; 5 6 import java.util.*; 7 import java.util.logging.*; 8 import net.nutch.util.LogFormatter; 9 10 16 public class ThreadPool { 17 21 class TaskThread extends Thread { 22 26 public void run() { 27 while (true) { 28 Runnable r = obtainJob(); 29 if (r == null) { 30 break; 31 } 32 try { 33 r.run(); 34 } catch (Exception 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 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 70 Runnable obtainJob() { 71 Runnable 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 ie) { 80 } 81 82 if (jobs.size() > 0) { 83 job = (Runnable ) jobs.firstElement(); 84 jobs.removeElementAt(0); 85 } 86 } 87 } 88 89 if (running) { 90 return job; 92 } else { 93 return null; 95 } 96 } 97 98 102 public void addJob(Runnable runnable) { 103 synchronized (jobs) { 104 jobs.add(runnable); 105 jobs.notifyAll(); 106 } 107 } 108 109 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 |