Overview

Chapter 17 - The BooleanLock Utility

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Inter-Thread Signaling Using BooleanLock
BooleanLock can be a useful tool for signaling between two or more threads. For example, if one thread needs to wait for another thread to complete a task before proceeding, it can invoke the waitUntilTrue() method. When the other thread completes the task, that thread would invoke setValue(true) . Using BooleanLock allows the wait/notify mechanism to remain hidden from the two threads. The class Signaling (see Listing 17.2) demonstrates this pattern.
Listing 17.2  Signaling.javaInter-thread Signaling with BooleanLock
1: public class Signaling extends Object {
2:     private BooleanLock readyLock;
3:
4:     public Signaling(BooleanLock readyLock) {
5:         this.readyLock = readyLock;
6:
7:         Runnable r = new Runnable() {
8:                 public void run() {
9:                     try {
10:                         runWork();
11:                     } catch (Exception x) {
12:                         // in case ANY exception slips through
13:                         x.printStackTrace();
14:                     }
15:                 }
16:             };
17:
18:         Thread internalThread = new Thread(r, internal);
19:         internalThread.start();
20:     }
21:
22:     private void runWork() {
23:         try {
24:             print(about to wait for readyLock to be true);
25:             readyLock.waitUntilTrue(0);   // 0 - wait forever
26:             print(readyLock is now true);
27:         } catch (InterruptedException x) {
28:             print( interrupted while waiting for readyLock +
29:                     to become true);
30:         }
31:     }
32:
33:     private static void print(String msg) {
34:         String name = Thread.currentThread().getName();
35:         System.err.println(name + : + msg);
36:     }
37:
38:     public static void main(String[] args) {
39:         try {
40:             print(creating BooleanLock instance);
41:             BooleanLock ready = new BooleanLock(false);
42:        
43:             print(creating Signaling instance);
44:             new Signaling(ready);
45:
46:             print(about to sleep for 3 seconds);
47:             Thread.sleep(3000);
48:
49:             print(about to setValue to true);
50:             ready.setValue(true);
51:             print(ready.isTrue()= + ready.isTrue());
52:         } catch (InterruptedException x) {
53:             x.printStackTrace();
54:         }
55:     }
56: }
The Signaling class has two parts : the instance-specific code (lines 231) and the static methods used for the demonstration (lines 3355). The static method print() (lines 3336) is used by both parts to print detailed messages.
In the main() method (lines 3855), an instance of BooleanLock is created (line 41). Next an instance of Signaling is created with a reference to the BooleanLock passed to its constructor (line 44). The main thread then sleeps for three seconds to allow the thread within the Signaling instance to start running (line 47). Finally, the main thread signals the internal thread by invoking setValue() with true (line 50) and verifies the change in state (line 51).
The constructor for Signaling (lines 420) uses the self-running object pattern and also stores the reference to the BooleanLock in readyLock (line 5).
In the runWork() method (lines 2230), the internal thread prints a message and then waits for the signal to proceed (line 25). When the BooleanLock is finally set to true , the internal thread moves on and prints another message (line 26) before returning from runWork() to die.
Listing 17.3 shows the likely output from Signaling . Your output should match closely. The only possible differences are that lines 3 and 4 might be swapped with each other and lines 6 and 7 might be swapped with each other.
Listing 17.3  Likely Output from Signaling
1: main: creating BooleanLock instance
2: main: creating Signaling instance
3: main: about to sleep for 3 seconds
4: internal: about to wait for readyLock to be true
5: main: about to setValue to true
6: main: ready.isTrue()=true
7: internal: readyLock is now true

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net