Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Thread example using join()

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Select the two correct answers.
(a) The first number printed is 13.
(b) The number 14 is printed before the number 22.
(c) The number 24 is printed before the number 21.
(d) The last number printed is 12.
(e) The number 11 is printed before the number 23.

Can anyone please explain me the above example.
Actually I dont understand the working of join() at all. Can anybody explain.
 
author
Posts: 23928
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

Please QuoteYourSources.

Henry
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry again.. Its from Khalid Mughal , review question- 13.16
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think B and D are correct options.
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here, in the above example 3 threads are created
1-main
2-createThread(20,main())
3-createThread(10,<threadcreated in point 2>

createThread method takes two arguments

1) int 2) ref to thread

and It creates and starts a new thread which joins itself end of second parameter thread passed.
--what ever the code inside run belongs to the newly created thread and what ever is outside,it belongs to main method

the threads are joined in way that main runs first, thread created in step 2 (green color) joins itself to main, thread created in step 3(red color) joins to thread created in step 2

option b is correct because: 14 is last statement for main method and 22 is statement printed by thread in step2 after joining itself to main method
option d is correct because: as it is the last statement after join in run method of last thread( step3 thread)

cheers
Don
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the concept of join method lies very much in its name. so what does join() method do ? it joins the CURRENTLY RUNNING THREAD WITH THE ONE ON WHICH IT IS INVOKED. to simplify say i have a join call in my main thread like this t1.join(); now think of this call as "JOIN ME( HERE ME IS CURRENTLY RUNNING THREAD) WITH T1 THREAD " . this will in a way PLACE the currently running thread i.e main thread in a QUEUE after t1. so the situation would be like this

T1 THREAD
|
|
|
MAIN THREAD.

first t1 thread will finish and then main thread will complete.

in your question let me give you a hint and then you can try to workout the flow yourself. there will be 3 threads in total in your program . one is the main thread and 2 resulting from the 2 calls of static createThread(). now due to join the threads will be like this

MAIN THREAD
|
|
|
Thread(say A) // resulting from the first createThread() call
|
|
|
Thread (say B) // resulting from second createThread() call


try to workout the flow and if still not able to get it we are here to help you.
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Don and Gurpreet for such a nice explanation.

But I still didn't get how we get the answer b,d?
Why (i+3); is not executing. Please elaborate.

I feel bad for not getting Threads at all. I read the chapter thrice and understand it properly but when comes to questions, unable to get the answer. Please suggest me what to do.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansi Agarwal wrote:Thanks Don and Gurpreet for such a nice explanation.

But I still didn't get how we get the answer b,d?
Why (i+3); is not executing. Please elaborate.

I feel bad for not getting Threads at all. I read the chapter thrice and understand it properly but when comes to questions, unable to get the answer. Please suggest me what to do.



i+3 is executing. why you thing it is not executing. i think you are confused because of the answer choices. the question is not to show the complete output , but it asks you the relative positions wrt to each other. why don't you run it and see the output for yourself
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic