Skip to content

Instantly share code, notes, and snippets.

@LBRapid
Created August 15, 2014 22:20
Show Gist options
  • Save LBRapid/00d5092d1360bb5e2d6b to your computer and use it in GitHub Desktop.
Save LBRapid/00d5092d1360bb5e2d6b to your computer and use it in GitHub Desktop.
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