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.
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)
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
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
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 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 ...