1 18 package org.apache.activemq.thread; 19 20 21 25 class DedicatedTaskRunner implements TaskRunner { 26 27 private final Task task; 28 private final Thread thread; 29 30 private final Object mutex = new Object (); 31 private boolean threadTerminated; 32 private boolean pending; 33 private boolean shutdown; 34 35 public DedicatedTaskRunner(Task task, String name, int priority, boolean daemon) { 36 this.task = task; 37 thread = new Thread (name) { 38 public void run() { 39 runTask(); 40 } 41 }; 42 thread.setDaemon(daemon); 43 thread.setName(name); 44 thread.setPriority(priority); 45 thread.start(); 46 } 47 48 50 public void wakeup() throws InterruptedException { 51 synchronized( mutex ) { 52 if( shutdown ) 53 return; 54 pending=true; 55 mutex.notifyAll(); 56 } 57 } 58 59 64 public void shutdown(long timeout) throws InterruptedException { 65 synchronized(mutex){ 66 shutdown=true; 67 pending=true; 68 mutex.notifyAll(); 69 70 if(!threadTerminated){ 72 mutex.wait(timeout); 73 } 74 } 75 } 76 77 81 public void shutdown() throws InterruptedException { 82 shutdown(0); 83 } 84 85 private void runTask() { 86 87 try { 88 while( true ) { 89 90 synchronized (mutex) { 91 pending=false; 92 if( shutdown ) { 93 return; 94 } 95 } 96 97 if( !task.iterate() ) { 98 synchronized (mutex) { 100 while( !pending ) { 101 mutex.wait(); 102 } 103 } 104 } 105 106 } 107 108 } catch (InterruptedException e) { 109 Thread.currentThread().interrupt(); 111 } finally { 112 synchronized (mutex) { 115 threadTerminated=true; 116 mutex.notifyAll(); 117 } 118 } 119 } 120 } 121 | Popular Tags |