Created
February 2, 2012 01:14
-
-
Save rhsdev/1720732 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
| 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