Created
May 22, 2012 06:22
-
-
Save write2munish/2767054 to your computer and use it in GitHub Desktop.
Java Threading Synchronization example
This file contains hidden or 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 Game { | |
public static void main(String args[]) { | |
PingPong table = new PingPong(); | |
Thread alice = new Thread(new Player("bob", table)); | |
Thread bob = new Thread(new Player("alice", table)); | |
alice.setName("alice"); | |
bob.setName("bob"); | |
alice.start(); // alice starts playing | |
bob.start(); // bob starts playing | |
try { | |
// Wait 5 seconds | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
} | |
table.hit("DONE"); // cause the players to quit their threads. | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment