Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://stackoverflow.com/questions/3605476/java-help-needed-sharing-an-object-between-two-threads-and-main-program
- public class SO3605476
- {
- public static void main(String[] args)
- {
- final int[] arr = new int[100];
- Thread t1 = new Thread() {
- public void run() {
- for (int i = 0; i < arr.length * 4000; i++) {
- //synchronized (arr) {
- arr[i % arr.length]--;
- //}
- }
- }
- };
- Thread t2 = new Thread() {
- public void run() {
- for (int i = 0; i < arr.length * 4000; i++) {
- //synchronized (arr) {
- arr[i % arr.length]++;
- //}
- }
- }
- };
- t1.start(); t2.start();
- try {
- t1.join(); t2.join(); // Wait until the threads finish
- }
- catch (InterruptedException e) { assert(false); }
- for (int i = 0; i < arr.length; i++) {
- System.out.printf("%d ", arr[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement