KickJava   Java API By Example, From Geeks To Geeks.

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


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
34 /**
35  * This object is used by a thread pool to control the termination of the jobs.
36  * A member of the ThreadPool class uses this object as a lock and perfoms
37  * wait call.
38  */

39 public class EndControler {
40
41     /** The number of active threads currently awake. */
42     private int numberOfAwakeThreads = 0;
43
44     /** This boolean keeps track of if the very first thread has started or not. This prevents
45      * this object from falsely reporting that the ThreadPool is done, just because the first
46      * thread has not yet started.
47      */

48     private boolean started = false;
49
50     /** Suspends the current thread until all the pending jobs in the ThreadPool are done. */
51     synchronized public void waitDone() {
52         try {
53             while (this.numberOfAwakeThreads > 0) {
54                 this.wait();
55             }
56         }
57         catch (InterruptedException JavaDoc e) { System.err.println("InterruptedException"); }
58     }
59
60 // /** Waits for the first thread to start. */
61
// synchronized public void waitBegin() {
62
//// Thread.dumpStack();
63
// this.started=true;
64
// // try {
65
// // while (!this.started) {
66
//// System.out.println("EndControler.waitBegin() started " + this.started);
67
// // this.wait();
68
// //} }
69
// // catch (InterruptedException e) { System.err.println("InterruptedException"); }
70
// }
71

72     /** A ThreadInThePool object calls this method to indicate it has started a job. */
73     synchronized public void jobStart() {
74 // Thread.dumpStack();
75
this.numberOfAwakeThreads++;
76         this.started = true;
77         this.notify();
78     }
79
80     /** A ThreadInThePool object calls this method to indicate it has finished a job. */
81     synchronized public void jobFinish() {
82 // Thread.dumpStack();
83
this.numberOfAwakeThreads--;
84         this.notify();
85     }
86
87     /** Resets the controler to its initial state (no job awake). */
88     synchronized public void reset() {
89         this.numberOfAwakeThreads = 0;
90     }
91
92 }
93
Popular Tags