
Daniel Trebbien
By: a guest on
Aug 30th, 2010 | syntax:
Java | size: 0.86 KB | hits: 51 | expires: Never
// 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();
}
}