1 package org.sapia.ubik.net; 2 3 4 18 public abstract class PooledThread extends Thread { 19 private Pool _pool; 20 private Object _task; 21 private boolean _shutdown; 22 private boolean _aquired; 23 24 protected PooledThread() { 25 } 26 27 28 final void setOwner(Pool pool) { 29 _pool = pool; 30 } 31 32 39 public final synchronized void exec(Object task) { 40 _task = task; 41 notify(); 42 } 43 44 47 public void shutdown() { 48 _shutdown = true; 49 interrupt(); 50 } 51 52 boolean isShutdown() { 53 return _shutdown; 54 } 55 56 void acquire() { 57 _aquired = true; 58 } 59 60 void release() { 61 _aquired = false; 62 } 63 64 public final synchronized void run() { 65 while (true) { 66 while ((_task == null) && !_shutdown) { 67 try { 68 wait(); 69 } catch (InterruptedException e) { 70 _task = null; 71 72 if (_aquired) { 73 _pool.release(this); 74 75 return; 76 } 77 } 78 } 79 80 if (_shutdown) { 81 _task = null; 82 83 if (_aquired) { 84 _pool.release(this); 85 } 86 87 return; 88 } 89 90 try { 91 doExec(_task); 92 } catch (Exception e) { 93 } 95 96 _task = null; 97 _pool.release(this); 98 } 99 } 100 101 113 protected abstract void doExec(Object task); 114 } 115 | Popular Tags |