I used Threaad.sleep and also used a static variable in the class to merge the results
Now I want to know if its possible to get separately the similar values returned by each thread

1 1

2 2

Post by:John Vorwald
, Ranch Hand

Sep 26, 2011 19:19:25

Try a semaphore with one permit, look for producer / consumer examples...

Post by:Sandeep Kumar B
, Ranch Hand

Sep 28, 2011 16:31:04

thanks, that helps

Post by:Rohan Dhapodkar
, Greenhorn

Oct 13, 2011 11:01:12

John Vorwald wrote:Try a semaphore with one permit, look for producer / consumer examples...

Semaphore is not perfect solution to this problem. Semaphore is to guarantee that specified number of threads can only enter into restricted area but not their ordering.
Below code will work with high sleep time 100 but if you will reduce sleep from 100ms to 1ms, you can notice a difference.

without semaphore, cyclic barrier, the following code produced desired result for several runs.

Post by:Rohan Dhapodkar
, Greenhorn

Oct 22, 2011 21:44:18

Harsha Smith wrote:without semaphore, cyclic barrier, the following code produced desired result for several runs.

This code is not perfect. Check results given below. Also what if i don;t want to put any sleep ? Above mentioned code execution is entirely based on Thread scheduler and it's not guaranteed to work always.
Try your code with just 3 threads and you will see difference.