This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static synchronized void someMethod() { | |
... | |
} |
Consider the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StaticMethodSynchronisation { | |
public static synchronized void foo() throws InterruptedException { | |
System.out.print("I am in foo: "); | |
for (int i = 0; i < 10; i++) { | |
System.out.print(i + " "); | |
Thread.sleep(100); | |
} | |
System.out.println("done with foo..."); | |
} | |
public static synchronized void bar() throws InterruptedException { | |
System.out.print("I am in bar: "); | |
for (int i = 0; i < 10; i++) { | |
System.out.print((char) (65 + i) + " "); | |
Thread.sleep(100); | |
} | |
System.out.println("done with bar..."); | |
} | |
public static void main(String[] args) { | |
// Create 3 threads to run foo. | |
for (int i = 0; i < 3; i++) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
foo(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
// Create 3 threads to run bar. | |
for (int i = 0; i < 3; i++) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
bar(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I am in foo: 0 1 2 3 4 5 6 7 8 9 done with foo... | |
I am in bar: A B C D E F G H I J done with bar... | |
I am in bar: A B C D E F G H I J done with bar... | |
I am in bar: A B C D E F G H I J done with bar... | |
I am in foo: 0 1 2 3 4 5 6 7 8 9 done with foo... | |
I am in foo: 0 1 2 3 4 5 6 7 8 9 done with foo... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StaticMethodSynchronisation { | |
static Lock FOOLOCK = new ReentrantLock(); | |
static Lock BARLOCK = new ReentrantLock(); | |
public static void foo() throws InterruptedException { | |
try { | |
FOOLOCK.lock(); | |
System.out.print("I am in foo: "); | |
for (int i = 0; i < 10; i++) { | |
System.out.print(i + " "); | |
Thread.sleep(100); | |
} | |
System.out.println("done with foo..."); | |
} finally { | |
FOOLOCK.unlock(); | |
} | |
} | |
public static void bar() throws InterruptedException { | |
try { | |
BARLOCK.lock(); | |
System.out.print("I am in bar: "); | |
for (int i = 0; i < 10; i++) { | |
System.out.print((char) (65 + i) + " "); | |
Thread.sleep(100); | |
} | |
System.out.println("done with bar..."); | |
} finally { | |
BARLOCK.unlock(); | |
} | |
} | |
public static void main(String[] args) { | |
// Create 3 threads to run foo. | |
for (int i = 0; i < 3; i++) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
foo(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
// Create 3 threads to run bar. | |
for (int i = 0; i < 3; i++) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
bar(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I am in foo: 0 I am in bar: A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J done with foo... | |
I am in foo: 0 done with bar... | |
I am in bar: A 1 B 2 C 3 D 4 E 5 F 6 G 7 H I 8 9 J done with foo... | |
done with bar... | |
I am in foo: 0 I am in bar: A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J done with foo... | |
done with bar... |
Locks also allows multiple methods to be tied together in all kinds of arrangements. For example, if we write a method called fooFriend() and use FOOLOCK to lock it, only one thread can be in the critical sections of foo() and fooFriend() at the same time. This makes a Java lock a rather powerful construct.
The try-finally finally block is rather important and ensures that the lock is releases no matter what. A situation may arise where an exception is thrown inside the critical section and the code will go into a deadlock as the lock is still owned by the original thread.
Indeed synchronized can be a rather embarrassing monster when it rears it's ugly head.
No comments:
Post a Comment