Re: java concurrency problem
Re: java concurrency problem
What does your code attempt to do?
Re: java concurrency problem
I want to multiply an array using two threads (which should be done using two cores of a C2D CPU).
The program prints, for example: "0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, Finished, 0.0, 2.0, 4.0, 6.0, 8.0, 5.0, 6.0, 7.0, 8.0, 9.0, Finished" or something similar. I except a result like this:
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, Finished, Finished, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0 and that make sense.
Maybe there should be:
Code :
t1.start();
t2.start();
t1.join();
t2.join();
Re: java concurrency problem
What happens when you add the join() calls?
The threads only have to do 10 loops. The first thread could finish before the second thread was started.
If you want overlaps between the two threads, try looping a lot more times.
Add some printlns in the run to show when the threads start.
Re: java concurrency problem
Ok, I added println to the run:
Code :
public void run()
{
System.out.println(Thread.currentThread());
for(int i = startIndex; i < endIndex; i++)
{
tab[i] = i * 2;
}
System.out.println("Finished");
}
With join() calls the program prints: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, Thread[Thread-0,5,main], Finished, Thread[Thread-1,5,main], Finished, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0 and that make sense.