1 28 package de.nava.informa.utils.toolkit; 29 30 50 public abstract class WorkerThread extends Thread { 51 52 53 private static final int IDLE_SLEEP_TIME = 20000; 54 55 private boolean busy = false; 56 private boolean terminate = false; 57 58 private JobSourceIF jobSource; 59 private ChannelRecord channelRecord; 60 61 66 public WorkerThread(String name) { 67 super(name); 68 } 69 70 75 public final void setJobSource(JobSourceIF source) { 76 this.jobSource = source; 77 } 78 79 82 public final void terminate() { 83 this.terminate = true; 84 synchronized (this) { 85 notifyAll(); 86 } 87 } 88 89 94 public final boolean isBusy() { 95 return busy; 96 } 97 98 104 public final synchronized boolean startJob(ChannelRecord rec) { 105 boolean accepted = false; 106 107 if (!this.busy) { 108 synchronized (this) { 109 this.busy = true; 110 channelRecord = rec; 111 notifyAll(); 112 } 113 accepted = true; 114 } 115 116 return accepted; 117 } 118 119 124 public final ChannelRecord getChannelInProcess() { 125 return channelRecord; 126 } 127 128 141 public final void run() { 142 while (!terminate) { 144 try { 145 if (!terminate) { 148 synchronized (this) { 149 this.wait(IDLE_SLEEP_TIME); 150 } 151 } 152 } catch (InterruptedException e) { 153 } 155 156 doJob(); 157 } 158 } 159 160 163 private void doJob() { 164 while (channelRecord != null) { 166 processRecord(channelRecord); 167 168 channelRecord = null; 170 if (!terminate && jobSource != null) { 171 channelRecord = jobSource.getNextJob(); 172 } 173 } 174 175 synchronized (this) { 177 busy = (channelRecord != null); 178 } 179 } 180 181 186 protected abstract void processRecord(ChannelRecord record); 187 } 188 | Popular Tags |