1 8 9 package foxtrot.workers; 10 11 import java.util.List ; 12 import java.util.LinkedList ; 13 14 import foxtrot.Task; 15 16 26 public class MultiWorkerThread extends SingleWorkerThread 27 { 28 private final List runners = new LinkedList (); 29 30 protected String getThreadName() 31 { 32 return "Foxtrot Multi Worker Thread Runner #" + nextSequence(); 33 } 34 35 protected void run(final Task task) 36 { 37 Thread thread = new Thread (Thread.currentThread().getThreadGroup(), new Runnable () 40 { 41 public void run() 42 { 43 try 44 { 45 synchronized (MultiWorkerThread.this) 46 { 47 runners.add(Thread.currentThread()); 48 } 49 50 runTask(task); 51 } 52 finally 53 { 54 synchronized (MultiWorkerThread.this) 55 { 56 runners.remove(Thread.currentThread()); 57 } 58 } 59 } 60 }, getThreadName()); 61 thread.setDaemon(true); 62 thread.start(); 63 if (debug) System.out.println("Started WorkerThread " + thread); 64 } 65 66 public boolean isWorkerThread() 67 { 68 synchronized (this) 69 { 70 return runners.contains(Thread.currentThread()); 71 } 72 } 73 74 boolean hasPendingTasks() 75 { 76 synchronized (this) 77 { 78 return super.hasPendingTasks() || runners.size() > 0; 79 } 80 } 81 } 82 | Popular Tags |