KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > pool > thread > PoolableThreadImpl


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.pool.thread;
8
9 import org.jfox.pool.ObjectPool;
10
11
12 /**
13  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
14  */

15
16 public class PoolableThreadImpl extends Thread JavaDoc implements PoolableThread {
17     private boolean available = false;
18     private ThreadTask task = null;
19     private volatile boolean running = false;
20     private static volatile long threadId = 0L;
21     private volatile boolean waiting = false;
22
23     public PoolableThreadImpl() {
24         super("PoolableThread: " + threadId++);
25         start();
26         while(!waiting) { // 为了防止该先程还没有启动完毕就返回,造成死锁
27
synchronized(this) {
28                 try {
29                     this.wait(1L);
30                 }
31                 catch(InterruptedException JavaDoc e) {
32                     e.printStackTrace();
33                 }
34             }
35         }
36     }
37
38     public void run() {
39         running = true;
40         do {
41             if(!running) break;
42             try {
43                 synchronized(this) {
44                     waiting = true;
45                     wait();
46                 }
47             }
48             catch(InterruptedException JavaDoc e) {
49                 e.printStackTrace();
50                 waiting = false;
51                 if(!running || task == null) break;
52             }
53
54             waiting = false; // have wakeup
55
if(task == null) break; // maybe wakeup by discard() method
56
try {
57                 task.getTask().run();
58             }
59             catch(Exception JavaDoc e) {
60             }
61             finally {
62                 // must restore the WorkThread to object pool
63
synchronized(this) {
64                     ObjectPool pool = task.getPool();
65                     synchronized(pool) {
66                         pool.restoreObject(this);
67                     }
68                 }
69             }
70
71         } while(true);
72     }
73
74     public void activate() throws Exception JavaDoc {
75 // System.out.println("PoolableThreadImpl.activate");
76
available = true;
77         running = true;
78
79     }
80
81     public void passivate() throws Exception JavaDoc {
82 // System.out.println("PoolableThreadImpl.passivate");
83
available = false;
84         task = null;
85     }
86
87     public boolean isAvailable() {
88         return available;
89     }
90
91     public void setTask(ThreadTask task) {
92 // System.out.println("PoolableThreadImpl.setTask" + getName() + " waiting " + waiting + ", running " + running );
93
if(waiting == true) {
94             this.task = task;
95             synchronized(this) {
96                 notify();
97             }
98         }
99     }
100
101     public void discard() {
102         running = false;
103         synchronized(this) {
104             notifyAll();
105         }
106         task.getPool().removeObject(this);
107     }
108
109     // thread will not sleep , so will not timeout
110
public long getSleepTime() {
111         return 0;
112     }
113
114 }
Popular Tags