Source code: com/yaftp/utils/RunnerWaiter.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.utils;
21 /**
22
23 Copyright Jean-Yves MENGANT 1998,1999,2000
24
25 the RunnerWaiter class is a scheduling class using
26 the basic TokenSemaphore system.
27
28 it can be used every time a Thread (The RUNNER) needs to
29 Synchronize another thread(The WAITER) using the following
30 rules :
31
32 THREAD 1 (starts and synchonizes THREAD 2 +
33 waits until THREAD 2)
34 startRUNNER() ;
35 DoWhatYouWant ;
36 stopRUNNER() ; // ==> BLOCK AND START WAITER
37
38 THREAD 2 (waits upon THREAD 1 initial completion
39 and start processing BLOCKING THREAD 1)
40 startWAITER() ; // BLOCK until RUNNER reschedules
41 DoWhatYouWant() ;
42 stopWAITER() ;
43
44 FOR A FULL EXAMPLE SEE The main static class of
45 TokenSemaphore which implements a full test of that
46 guy.
47
48 @author Jean-Yves MENGANT
49
50 */
51
52 public class RunnerWaiter
53 implements java.io.Serializable {
54
55 // JAVA BEAN SERIALIZATION VERSION ID NUMBER
56 static final long serialVersionUID = UtilsVersion.CURRENT_PRODUCT_VERSION ;
57 // static final long serialVersionUID = 2754969633580486221L;
58
59 private transient TokenSemaphore theRunnerFlag ;
60 private transient TokenSemaphore theWaiterFlag ;
61
62
63 public RunnerWaiter()
64 {
65 super() ;
66 theRunnerFlag = new TokenSemaphore("theRUNNER",false) ;
67 theWaiterFlag = new TokenSemaphore("theWAITER",false) ;
68 }
69 /**
70
71 Start the Runner
72
73 */
74 public void startRUNNER()
75 {
76 theRunnerFlag.waitForSemaphore() ; // START OWNING RUN HERE
77 }
78
79 public void stopRUNNER()
80 {
81 while ( ! theWaiterFlag.isBusy() )
82 Thread.yield() ; // THIS GUY SYNCHRONIZES THE ALL
83 theRunnerFlag.freeSemaphore() ; // UNBLOCK the WAITER
84 theWaiterFlag.waitForSemaphore() ; // BLOCK here on the WAITER
85 theWaiterFlag.freeSemaphore() ; // Release this one ASAP
86 }
87
88 public void startWAITER()
89 {
90 while ( ! theRunnerFlag.isBusy() )
91 Thread.yield() ; // THIS GUY SYNCHRONIZES THE ALL
92 theWaiterFlag.waitForSemaphore() ; // START OWNING WAIT
93 theRunnerFlag.waitForSemaphore() ; // BLOCKING HERE
94 theRunnerFlag.freeSemaphore() ; // Release this one ASAP
95 }
96
97 public void stopWAITER()
98 {
99 theWaiterFlag.freeSemaphore() ;
100 }
101
102
103 } ;