Skip to content

Instantly share code, notes, and snippets.

@rhsdev
Created February 2, 2012 01:14
Show Gist options
  • Select an option

  • Save rhsdev/1720732 to your computer and use it in GitHub Desktop.

Select an option

Save rhsdev/1720732 to your computer and use it in GitHub Desktop.
class test1 implements Runnable {
long start;
long stop;
static long sum = 0;
static Object lock = new Object();
public test1(long start, long stop) {
this.start = start;
this.stop = stop;
this.sum = sum;
}
public void run() {
synchronize(lock) {
for(int i = start; i < stop; i++) {
sum += i;
}
}
}
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new test1(0,5000));
Thread t2 = new Thread(new test1(5001,10000));
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment