• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, Threads are not guaranteed to go in any particular order.
 
Greenhorn
Posts: 24
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic