Skip to content

Instantly share code, notes, and snippets.

@DanGe42
Created February 6, 2014 06:51
Show Gist options
  • Save DanGe42/8839424 to your computer and use it in GitHub Desktop.
Save DanGe42/8839424 to your computer and use it in GitHub Desktop.
A simple program showing Java synchronization behavior.
Starting thread.
Starting thread.
Starting thread.
Starting thread.
Starting thread.
Starting thread.
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Entered guarded block
Starting thread.
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Entered guarded block
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Starting thread.
Entered guarded block
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Starting thread.
Entered guarded block
Entered guarded block
Starting thread.
Entered guarded block
Starting thread.
Entered guarded block
Entered guarded block
Starting thread.
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Done!
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Entered guarded block
Notifying everyone.
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Done!
Exit
public class SyncTest {
public void test1(int x) throws InterruptedException {
synchronized (this) {
System.out.println("Entered guarded block");
if (x != 42) {
wait();
}
System.out.println("Done!");
}
}
public void test2() {
synchronized (this) {
System.out.println("Notifying everyone.");
notifyAll();
}
}
public static void main(String[] args) throws InterruptedException {
final SyncTest sync = new SyncTest();
for (int i = 0; i < 50 ; i++) {
final int x = i;
(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Starting thread.");
try {
sync.test1(x);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
}
}
})).start();
}
Thread.sleep(2000);
(new Thread(new Runnable() {
@Override
public void run() {
sync.test2();
}
})).start();
Thread.sleep(2000);
System.out.println("Exit");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment