KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > group > threadpool > ThreadPool


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.group.threadpool;
32
33 import org.objectweb.proactive.core.group.AbstractProcessForGroup;
34
35 import java.util.ArrayList JavaDoc;
36
37
38 /**
39  * A thread pool is a set of threads waiting for jobs. The thread in the pool
40  * are created one time and re-used for new jobs.
41  */

42 public class ThreadPool {
43
44     /** The set of threads. */
45     private Thread JavaDoc[] threads = null;
46
47     /** The queue of pending jobs waiting to be served by a thread. */
48     private ArrayList JavaDoc pendingJobs = null;
49
50     /** The controler that is looking for the end of jobs to perform. */
51     protected EndControler controler = new EndControler();
52
53     /** the member to thread ratio, i.e. the number of members served by a single thread */
54     private int memberToThreadRatio = 4;
55
56     /** Builds a ThreadPool.
57      * By default, the number of thread in the pool is 10.
58      */

59     public ThreadPool() {
60         this(1);
61     }
62
63     /** Builds a ThreadPool, specifying the number of thread to create.
64      * @param <code>size<code> the number of thread in the thread pool.
65      */

66     public ThreadPool(int size) {
67         this.threads = new ThreadInThePool[size];
68         this.pendingJobs = new ArrayList JavaDoc(size);
69         for (int i = 0; i < this.threads.length; i++) {
70             this.threads[i] = new ThreadInThePool(this);
71             this.threads[i].start();
72         }
73     }
74
75     /**
76      * Creates the needed threads for this ThreadPool
77      * @param <code> number </code> the number of threads needed
78      */

79     protected void createThreads(int number) {
80         this.threads = new ThreadInThePool[number];
81         for (int i = 0; i < this.threads.length; i++) {
82             this.threads[i] = new ThreadInThePool(this);
83             this.threads[i].start();
84         }
85     }
86
87     /**
88      * Check wether the number of threads in this threadpool
89      * is sufficient compared to the number of members in the group
90      * @param <code> members </code> the number of members in the group
91      */

92     public void checkNumberOfThreads(int members) {
93         if (members > (this.memberToThreadRatio * this.threads.length)) {
94             int i;
95             int f = (int) Math.ceil(((float) members) / ((float) this.memberToThreadRatio));
96             Thread JavaDoc[] tmp = new Thread JavaDoc[f];
97
98             for (i = 0; i < this.threads.length; i++) {
99                 tmp[i] = this.threads[i];
100             }
101             for (; i < f; i++) {
102                 tmp[i] = new ThreadInThePool(this);
103                 tmp[i].start();
104             }
105             this.threads = tmp;
106         } else if (members < (this.memberToThreadRatio * this.threads.length)) {
107             int i;
108             int f = (int) Math.ceil(((float) members) / ((float) this.memberToThreadRatio));
109             Thread JavaDoc[] tmp = new Thread JavaDoc[f];
110             for (i = 0; i < f; i++) {
111                 tmp[i] = this.threads[i];
112             }
113             for (; i < this.threads.length; i++) {
114                 this.threads[i] = null;
115             }
116             this.threads = tmp;
117         }
118     }
119
120     /**
121      * Modifies the number of members served by one thread
122      * @param i - the new ratio
123      */

124     public void ratio(int i) {
125         this.memberToThreadRatio = i;
126     }
127
128     /** Adds a job to the pending queue of the thread pool. */
129     public synchronized void addAJob(AbstractProcessForGroup r) {
130         this.controler.jobStart();
131         this.pendingJobs.add(r);
132         this.notify();
133     }
134
135     /** Picks up new job to execute in the pending queue.
136      * @return A new job to execute
137      */

138     public synchronized Runnable JavaDoc getJobForThePendingQueue() {
139         try {
140             while (!this.pendingJobs.iterator().hasNext()) {
141                 this.wait();
142             }
143             Runnable JavaDoc r = (Runnable JavaDoc) this.pendingJobs.iterator().next();
144             this.pendingJobs.remove(r);
145             return r;
146         } catch (InterruptedException JavaDoc e) {
147             this.controler.jobFinish();
148             return null;
149         }
150     }
151
152     /** Waits until the ThreadPool has no more job to execute (pending queue is empty). */
153     public void complete() {
154         //this.controler.waitBegin();
155
this.controler.waitDone();
156     }
157
158     /** Cleanly destroys a ThreadPool object */
159     public void finalize() {
160         this.controler.reset();
161         for (int i = 0; i < threads.length; i++) {
162             this.threads[i].interrupt();
163             this.controler.jobStart();
164             this.threads[i].destroy();
165         }
166         this.controler.waitDone();
167     }
168 }
169
Popular Tags