Created
August 15, 2014 22:20
-
-
Save LBRapid/00d5092d1360bb5e2d6b to your computer and use it in GitHub Desktop.
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 Counting { | |
public static void main(String[] args) throws InterruptedException { | |
class Counter { | |
private int count = 0; | |
public synchronized void increment() { ++count; } | |
public int getCount() { return count; } | |
} | |
final Counter counter = new Counter(); | |
class CountingThread extends Thread { | |
public void run() { | |
for (int x = 0; x < 10000; ++x) | |
counter.increment(); | |
} | |
} | |
CountingThread t1 = new CountingThread(); | |
CountingThread t2 = new CountingThread(); | |
t1.start(); t2.start(); | |
t1.join(); t2.join(); | |
System.out.println(counter.getCount()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment