View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.network;
20  
21  /***
22   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
23   * @since 0.3
24   */
25  public class MMiniPipe {
26  
27  	/***
28  	 * Create an instance of MMiniPipe initializing the token to free. Means that
29  	 * the next call to method <code>take()</code> would'nt block the calling
30  	 * thread.
31  	 * 
32  	 * @see MMiniPipe#take()
33  	 * @see #MMiniPipe(boolean)
34  	 */
35  	public MMiniPipe() {
36  		this(false);
37  	}
38  
39  	/***
40  	 * Create an instance of MMiniPipe initializing the token to the specified
41  	 * boolean. Means that the next call to method <code>take()</code> would'nt
42  	 * block the calling thread only if the specified <code>taken</code> is
43  	 * false.
44  	 * 
45  	 * @param taken
46  	 *          one token critical resource
47  	 * @see MMiniPipe#take()
48  	 */
49  	public MMiniPipe(boolean taken) {
50  		this.taken = taken;
51  	}
52  
53  	/***
54  	 * Try to take this token. If the token is already token, the current thread
55  	 * would be blocked until a threadd release the requested token.
56  	 */
57  	public synchronized void take() {
58  		try {
59  			while (taken) {
60  				wait(200);
61  			}
62  			taken = true;
63  		} catch (InterruptedException e) {
64  			System.out.println("mini pipe error");
65  			// throw new InternalError("Interrupted in needToSend method");
66  		}
67  	}
68  
69  	/***
70  	 * Try to take this token. If the token is already token, the current thread
71  	 * would be blocked until a threadd release the requested token.
72  	 * 
73  	 * @return true if has been token.
74  	 */
75  	public synchronized boolean takeNoBlock() {
76  		if (!taken) {
77  			taken = true;
78  			return false;
79  		}
80  		return false;
81  	}
82  
83  	/***
84  	 * Try to take this token. If the token is already token, the current thread
85  	 * would be blocked until a threadd release the requested token.
86  	 */
87  	public synchronized void release() {
88  		taken = false;
89  		notify();
90  	}
91  
92  	/***
93  	 * This boolean represents the token. When it's value is <code>false</code>
94  	 * that's mean the token is free, so any thread can take it without being
95  	 * blocked. When it' value is <code>true</code> the next thread would take
96  	 * this token would be blocked until the current owner release it.
97  	 */
98  	protected boolean taken;
99  
100 }