aspose file tools*
The moose likes Threads and Synchronization and the fly likes Break the loop using threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Break the loop using threads" Watch "Break the loop using threads" New topic
Author

Break the loop using threads

Sandeep Kumar B
Ranch Hand

Joined: Jul 10, 2011
Posts: 41
I have a class with method calling getValues

getValues would print the values from 1 to 10

I make two objects and what gets printed is
1
2
3
...
\
..
.

1
2
3
.
..
10

What I want is

1
1
2
2
3
3
...
...
10
10



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
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Try a semaphore with one permit, look for producer / consumer examples...
Sandeep Kumar B
Ranch Hand

Joined: Jul 10, 2011
Posts: 41
thanks, that helps
Rohan Dhapodkar
Greenhorn

Joined: Jun 27, 2011
Posts: 23
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.

output with 100 ms.
0 Thread-0
0 Thread-1
1 Thread-0
1 Thread-1
2 Thread-0
2 Thread-1
3 Thread-0
3 Thread-1
4 Thread-0
4 Thread-1
5 Thread-0
5 Thread-1
6 Thread-0
6 Thread-1
7 Thread-0
7 Thread-1
8 Thread-0
8 Thread-1
9 Thread-0
9 Thread-1
10 Thread-0
10 Thread-1
11 Thread-0
11 Thread-1
12 Thread-0
12 Thread-1
13 Thread-0
13 Thread-1
14 Thread-0
14 Thread-1
15 Thread-0
15 Thread-1
16 Thread-0
16 Thread-1
17 Thread-0
17 Thread-1
18 Thread-0
18 Thread-1
19 Thread-0
19 Thread-1

Output with 1 ms.
0 Thread-0
1 Thread-0
0 Thread-1
2 Thread-0
1 Thread-1
3 Thread-0
2 Thread-1
4 Thread-0
3 Thread-1
5 Thread-0
4 Thread-1
6 Thread-0
5 Thread-1
7 Thread-0
6 Thread-1
8 Thread-0
7 Thread-1
9 Thread-0
8 Thread-1
10 Thread-0
9 Thread-1
11 Thread-0
10 Thread-1
12 Thread-0
11 Thread-1
13 Thread-0
12 Thread-1
14 Thread-0
13 Thread-1
15 Thread-0
14 Thread-1
16 Thread-0
15 Thread-1
17 Thread-0
16 Thread-1
18 Thread-0
17 Thread-1
19 Thread-0
18 Thread-1
19 Thread-1

Cyclic Barrier is perfect solution for such problems. This is gauranteed to work with or without Thread.sleep().



output:

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
quite informative, Rohan
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
without semaphore, cyclic barrier, the following code produced desired result for several runs.

Rohan Dhapodkar
Greenhorn

Joined: Jun 27, 2011
Posts: 23
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.

0 Thread-0
0 Thread-1
0 Thread-2
1 Thread-0
1 Thread-1
1 Thread-2
2 Thread-0
2 Thread-1
2 Thread-2
3 Thread-0
3 Thread-1
3 Thread-2
4 Thread-0
4 Thread-1
4 Thread-2
5 Thread-0
5 Thread-1
5 Thread-2
6 Thread-0
6 Thread-1
6 Thread-2
7 Thread-0
7 Thread-1
7 Thread-2
8 Thread-0
8 Thread-1
8 Thread-2
9 Thread-0
9 Thread-1
9 Thread-2
10 Thread-0
10 Thread-1
10 Thread-2
11 Thread-0
11 Thread-1
11 Thread-2
12 Thread-0
12 Thread-1
12 Thread-2
13 Thread-0
13 Thread-1
13 Thread-2
14 Thread-0
14 Thread-1
15 Thread-0
15 Thread-1
14 Thread-2
16 Thread-0
16 Thread-1
15 Thread-2
17 Thread-0
17 Thread-1
16 Thread-2
18 Thread-0
18 Thread-1
17 Thread-2
19 Thread-0
19 Thread-1
18 Thread-2
19 Thread-2


Even with just 2 threads out put is not perfect.
Check this
0 Thread-0
0 Thread-1
1 Thread-0
1 Thread-1
2 Thread-0
2 Thread-1
3 Thread-0
3 Thread-1
4 Thread-0
4 Thread-1
5 Thread-0
5 Thread-1
6 Thread-0
6 Thread-1
7 Thread-0
7 Thread-1
8 Thread-0
8 Thread-1
9 Thread-0
9 Thread-1
10 Thread-0
10 Thread-1
11 Thread-0
11 Thread-1
12 Thread-0
12 Thread-1
13 Thread-0
14 Thread-0
13 Thread-1
15 Thread-0
14 Thread-1
16 Thread-0
15 Thread-1
17 Thread-0
16 Thread-1
18 Thread-0
17 Thread-1
19 Thread-0
18 Thread-1
19 Thread-1

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Break the loop using threads