Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread example using join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread example using join()" Watch "Thread example using join()" New topic
Author

Thread example using join()

Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53


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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19001
    
  40


Please QuoteYourSources.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Sorry again.. Its from Khalid Mughal , review question- 13.16
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

I think B and D are correct options.
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

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
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Apr 11, 2012
Posts: 53
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread example using join()