Summary

Chapter 7 - Concurrent Access to Objects and Variables

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Using the Class-Level Lock in a synchronized Statement
The synchronized statement can also use a class-level lock. This can be useful if a static method runs for a long period of time. Additionally, it can be used to ensure that two static method calls by one thread are not interleaved with a call by another thread.
To lock on the class-level lock, use the following code
synchronized (ClassName. class ) {
    // body
}
where ClassName is replaced with the real name of the class you want to use. Class StaticBlock (see Listing 7.23) demonstrates this technique.
Listing 7.23  StaticBlock.javaUsing .class to Gain Access to the Class-Level Lock
1: public class StaticBlock extends Object {
2:     public static synchronized void staticA() {
3:         System.out.println(entering staticA());
4:
5:         try { Thread.sleep(5000); }
6:         catch (InterruptedException x) { }
7:
8:         System.out.println(leaving staticA());
9:     }
10:
11:     public static void staticB() {
12:         System.out.println(entering staticB());
13:
14:         synchronized (StaticBlock.class) {
15:             System.out.println(
16:                     in staticB() - inside sync block);
17:
18:             try { Thread.sleep(2000); }
19:             catch (InterruptedException x) { }
20:         }
21:
22:         System.out.println(leaving staticB());
23:     }
24:
25:     public static void main(String[] args) {
26:         Runnable runA = new Runnable() {
27:                 public void run() {
28:                     StaticBlock.staticA();
29:                 }
30:             };
31:
32:         Thread threadA = new Thread(runA, threadA);
33:         threadA.start();
34:
35:         try { Thread.sleep(200); }
36:         catch (InterruptedException x) { }
37:
38:         Runnable runB = new Runnable() {
39:                 public void run() {
40:                     StaticBlock.staticB();
41:                 }
42:             };
43:
44:         Thread threadB = new Thread(runB, threadB);
45:         threadB.start();
46:     }
47: }
In StaticBlock , the staticA() method (lines 29) is both static and synchronized . The staticB() method  (lines 1123) is static and contains a synchronized block (lines 1420). The object used to control access to this block is the Class object for StaticBlock and is found by using StaticBlock.class (line 14).
In main() (lines 2546), threadA is started and invokes staticA() (line 28). After a brief 200 millisecond break, threadB is started and invokes staticB() . While threadA is sleeping inside staticA() , threadB enters staticB() , prints a message, and blocks waiting to enter the synchronized statement block (line 14). When threadA returns from staticA() , threadB is able to get the class-level lock and completes staticB() .
Listing 7.24 shows the output produced when StaticBlock is run. Your output should match.
Listing 7.24  Output from StaticBlock (Your Output Should Match)
1: entering staticA()
2: entering staticB()
3: leaving staticA()
4: in staticB() - inside sync block
5: leaving staticB()
Notice that although threadB is able to enter staticB() (line 2), it cant enter the synchronized block (line 4) until threadA returns from staticA() (line 3). threadB blocks waiting for threadA to release the class-level lock.

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