01: package com.sun.portal.proxylet.client.common.server.util;
02:
03: import com.sun.portal.proxylet.client.common.server.util.ThreadPool;
04:
05: public class ReusableThread extends Thread {
06: private ThreadPool pool = null;
07: private Runnable runnable = null;
08: private boolean alive = true;
09: private long lastrun = 0;
10: private int used = 0;
11:
12: public ReusableThread(ThreadPool pool) {
13: this .pool = pool;
14: }
15:
16: public synchronized void setRunnable(Runnable runnable) {
17: this .runnable = runnable;
18: notify();
19: }
20:
21: public synchronized void terminate() {
22: alive = false;
23: notify();
24: }
25:
26: public long getLastRunTime() {
27: return lastrun;
28: }
29:
30: public int useCount() {
31: return used;
32: }
33:
34: public void run() {
35: while (alive) {
36: setName("ReusableThread: idle");
37:
38: while (runnable == null && alive) {
39: synchronized (this ) {
40: try {
41: wait();
42: } catch (InterruptedException ie) {
43: }
44: }
45: }
46:
47: if (alive) {
48: setName("ReusableThread: busy");
49: setPriority(Thread.NORM_PRIORITY);
50: lastrun = System.currentTimeMillis();
51: used++;
52: runnable.run();
53: runnable = null;
54: pool.put(this);
55: }
56: }
57: }
58: }
|