Created
February 6, 2014 06:51
-
-
Save DanGe42/8839424 to your computer and use it in GitHub Desktop.
A simple program showing Java synchronization behavior.
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
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 |
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 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