Advertisement
Guest User

Daniel Trebbien

a guest
Aug 30th, 2010
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. // http://stackoverflow.com/questions/3605476/java-help-needed-sharing-an-object-between-two-threads-and-main-program
  2. public class SO3605476
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         final int[] arr = new int[100];
  7.         Thread t1 = new Thread() {
  8.             public void run() {
  9.                 for (int i = 0; i < arr.length * 4000; i++) {
  10.                     //synchronized (arr) {
  11.                     arr[i % arr.length]--;
  12.                     //}
  13.                 }
  14.             }
  15.         };
  16.         Thread t2 = new Thread() {
  17.             public void run() {
  18.                 for (int i = 0; i < arr.length * 4000; i++) {
  19.                     //synchronized (arr) {
  20.                     arr[i % arr.length]++;
  21.                     //}
  22.                 }
  23.             }
  24.         };
  25.         t1.start(); t2.start();
  26.         try {
  27.             t1.join(); t2.join(); // Wait until the threads finish
  28.         }
  29.         catch (InterruptedException e) { assert(false); }
  30.         for (int i = 0; i < arr.length; i++) {
  31.             System.out.printf("%d ", arr[i]);
  32.         }
  33.         System.out.println();
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement