KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > pool > thread > ThreadObjectFactory


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.pool.thread;
16
17 import org.apache.commons.pool.BasePoolableObjectFactory;
18
19 /**
20  * A factory for creating {@link org.quickserver.util.pool.thread.ClientThread}
21  * instances.
22  * @author Akshathkumar Shetty
23  * @since 1.3
24  */

25 public class ThreadObjectFactory extends BasePoolableObjectFactory {
26     private ClientPool pool;
27     private static int instanceCount = 0;
28     private int id = -1;
29
30     public ThreadObjectFactory() {
31         super();
32         id = ++instanceCount;
33     }
34
35     public void setClientPool(ClientPool pool) {
36         this.pool = pool;
37     }
38
39     //Creates an instance that can be returned by the pool.
40
public Object JavaDoc makeObject() {
41         ClientThread ct = new ClientThread(pool, id);
42         ct.start();
43         return ct;
44     }
45
46     //Uninitialize an instance to be returned to the pool.
47
public void passivateObject(Object JavaDoc obj) {
48         ((ClientThread)obj).clean();
49     }
50
51     //Reinitialize an instance to be returned by the pool.
52
public void activateObject(Object JavaDoc obj) {
53     }
54     
55     //Destroys an instance no longer needed by the pool.
56
public void destroyObject(Object JavaDoc obj) {
57         if(obj==null) return;
58         Thread JavaDoc thread = (Thread JavaDoc) obj;
59         thread.interrupt();
60         thread = null;
61     }
62
63     //Ensures that the instance is safe to be returned by the pool.
64
public boolean validateObject(Object JavaDoc obj) {
65         if(obj==null) return false;
66         Thread JavaDoc thread = (Thread JavaDoc)obj;
67         return thread.isAlive();
68     }
69 }
70
Popular Tags