KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > util > ThreadStoreImpl


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.util;
32
33 /**
34  * <p>
35  * A straightford implementation of the threadstore interface.
36  * </p>
37  *
38  * @author ProActive Team
39  * @version 1.0, 2002/05
40  * @since ProActive 0.9.2
41  */

42 public class ThreadStoreImpl implements ThreadStore, java.io.Serializable JavaDoc {
43
44   private int counter;
45   private boolean defaultOpenState;
46   private transient boolean open;
47
48   /**
49    * Creates a new ThreadStore that is opened after creation.
50    */

51   public ThreadStoreImpl() {
52     this(true);
53   }
54
55
56   /**
57    * Constructor for ThreadStoreImpl.
58    * @param isOpened true is the store is opened after creation
59    */

60   public ThreadStoreImpl(boolean isOpened) {
61     defaultOpenState = isOpened;
62     open = defaultOpenState;
63   }
64
65
66   /**
67    * @see ThreadStore#threadCount()
68    */

69   public int threadCount() {
70     return counter;
71   }
72
73
74   /**
75    * @see ThreadStore#enter()
76    */

77   public synchronized void enter() {
78     while (!open) {
79       try {
80         wait();
81       } catch (InterruptedException JavaDoc e) {}
82     }
83     counter++;
84   }
85
86
87   /**
88    * @see ThreadStore#exit()
89    */

90   public synchronized void exit() {
91     counter--;
92     notifyAll();
93   }
94
95
96   /**
97    * @see ThreadStore#close()
98    */

99   public synchronized void close() {
100     open = false;
101     while (counter != 0 && !open) {
102       try {
103         wait();
104       } catch (InterruptedException JavaDoc e) {}
105     }
106   }
107
108
109   /**
110    * @see ThreadStore#open()
111    */

112   public synchronized void open() {
113     open = true;
114     notifyAll();
115   }
116
117
118   //
119
// -- PRIVATE METHODS -----------------------------------------------
120
//
121

122   private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
123     out.defaultWriteObject();
124   }
125
126   private void readObject(java.io.ObjectInputStream JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
127     in.defaultReadObject();
128     // set open to the default value
129
open = defaultOpenState;
130   }
131 }
132
Popular Tags