OnJava8-Examples/threads/SyncObject.java
2016-07-22 14:45:35 -06:00

49 lines
885 B
Java

// threads/SyncObject.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Synchronizing on another object
class DualSynch {
private Object syncObject = new Object();
public synchronized void f() {
for(int i = 0; i < 5; i++) {
System.out.println("f()");
Thread.yield();
}
}
public void g() {
synchronized(syncObject) {
for(int i = 0; i < 5; i++) {
System.out.println("g()");
Thread.yield();
}
}
}
}
public class SyncObject {
public static void main(String[] args) {
final DualSynch ds = new DualSynch();
new Thread() {
@Override
public void run() {
ds.f();
}
}.start();
ds.g();
}
}
/* Output:
g()
g()
g()
g()
g()
f()
f()
f()
f()
f()
*/