• Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain the output this example of thread join() method  RSS feed

 
shailesh shekhawat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit - see UseCodeTags for details]



output:
1
2
3
1
1
4
2
5
2
3
3
4
4
5
5
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailesh. Welcome to the Ranch!

What were you expecting to happen, and why? It's easier to explain things if we know what you don't understand.

One thing that might be relevant though: you wait up to a second and a half for the first thread to finish before starting the others. But the first thread will take longer that than, so you'll start the other threads before it finishes.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Threads are not guaranteed to go in any particular order.
 
David McMonigle
Greenhorn
Posts: 24
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some clarification might help. Do you not understand what the Join() method is doing or how the threads themselves are working?
 
shailesh shekhawat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i am able to understand is :
till 1500 miliseconds thread t1 will execute and print
1
2
3
and after that t2 execute it goes into sleep for 500 miliseconds and schedular picks up t2 and print
1
and after 500 miliseconds t1 will execute and print 1 again
and after that i am not able to understand as within interval of 500 ms it is printing 1,2,3 in console and after that it directly prints 3 values in a row without any interval (1,1,4)
how that possible please explain in detail?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shailesh shekhawat wrote:what i am able to understand is :
till 1500 miliseconds thread t1 will execute and print
1
2
3
and after that t2 execute it goes into sleep for 500 miliseconds and schedular picks up t2 and print
1
and after 500 miliseconds t1 will execute and print 1 again
and after that i am not able to understand as within interval of 500 ms it is printing 1,2,3 in console and after that it directly prints 3 values (1,1,4)
how that possible please explain in detail?


You are going to have to take into account the existence of t3, before you can understand the output -- as there is no way to explain why there are three of each number without accounting for t3.

Henry
 
shailesh shekhawat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry my mistake...i forgot to mention t3 .
after t2 ,t3 will execute and print 1 and what happens after that?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shailesh shekhawat wrote:sorry my mistake...i forgot to mention t3 .
after t2 ,t3 will execute and print 1 and what happens after that?


You probably wouldn't be able to tell the difference between the output for t2 and t3 as they were started at almost the same time, but the output for t1 should be easy as it is three numbers ahead. What are you having an issue with? Can you re-explain the flow, and tell us where your confusion is?

Henry
 
shailesh shekhawat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i am able to understand is :
till 1500 miliseconds thread t1 will execute and print
1
2
3
and after that t2 execute it goes into sleep for 500 miliseconds and schedular picks up t3 and print
1
and after 500 miliseconds t1 will execute and print 1 again
and after that i am not able to understand as within interval of 500 ms it is printing 1,2,3 in console and after that it directly prints 3 values in a row without any interval (1,1,4)
how that possible please explain in detail?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shailesh shekhawat wrote:and after 500 miliseconds t1 will execute and print 1 again

Where are you getting this idea from ? t1 will only ever print 1 once and that's the first 1 that you see.

shailesh shekhawat wrote:after that it directly prints 3 values in a row without any interval (1,1,4)

Once 1500ms have elapsed all 3 threads will be running and each will sleep for 500ms between printing of numbers, but each thread will not take it in turns to sleep 500ms. If each thread starts sleeping at exactly the same time, then all three threads will wake up 500ms later. It won't be the case of one thread sleeping 500ms, then the second thread sleeps, then the third thread sleeps. The 500ms will be the same 500ms for all three threads.

Why don't you add the thread names to your output (and maybe even the time) and see what the output is. Maybe that will help.

Here's one possible output
1: Thread-0: 501
2: Thread-0: 1001
3: Thread-0: 1501
4: Thread-0: 2001
1: Thread-2: 2001
1: Thread-1: 2001
5: Thread-0: 2501
2: Thread-2: 2501
2: Thread-1: 2501
3: Thread-2: 3001
3: Thread-1: 3001
4: Thread-2: 3501
4: Thread-1: 3501
5: Thread-1: 4001
5: Thread-2: 4001
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!