• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Break the loop using threads

 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a semaphore with one permit, look for producer / consumer examples...
 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, that helps
 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quite informative, Rohan
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without semaphore, cyclic barrier, the following code produced desired result for several runs.

 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic