| | face="Courier New" size=2>InterruptibleSyncBlock color=#010100 face=Arial size=2> is very similar to color=#010100 face="Courier New" size=2>SyncBlock color=#010100 face=Arial size=2>, except for a few enhancements inside the size=2>doStuff() method (lines 10–32). A new member variable face="Courier New" size=2>busyLock face=Arial size=2> has been added (line 3), and it is an instance of size=2>BooleanLock (line 7). It is used inside face="Courier New" size=2>doStuff() face=Arial size=2> to control concurrent access. The color=#010100 face="Courier New" size=2>waitToSetTrue() color=#010100 face=Arial size=2> method is invoked by each thread as it enters size=2>doStuff() (line 13). Because a timeout of face="Courier New" size=2>0 size=2> is passed, threads will wait forever to get their turn—or at least until they are interrupted. When one of the threads gets notified that it can set the state to face="Courier New" size=2>true size=2>, it does so and returns from face="Courier New" size=2>waitToSetTrue() face=Arial size=2>. All the other threads will get notified, but will see that some other thread has beat them and will go back to waiting inside size=2>waitToSetTrue(). The thread that gets to set face="Courier New" size=2>busyLock face=Arial size=2> to size=2>true then proceeds into the size=2>try block (lines 15–27). No matter how this thread leaves the color=#010100 face="Courier New" size=2>try color=#010100 face=Arial size=2> block (even by throwing an size=2>Exception or size=2>Error), it will enter the size=2>finally block (lines 28–31) and set size=2>busyLock back to size=2>false (line 30) to allow another thread to get into the exclusive section. | |
|
No comments:
Post a Comment