Which statements are true about the following code?
My understanding( which is wrong) says that this should be the output
My question is why thread is not started even on starting it at P1. I commented out join statement and it gave the same result as without it.
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.
In this code the main thread first calls createThread(20, Thread.currentThread()) method.
In this method first a new thread object is created in line 04,
then the main thread executes line 14, which prints 23 to the output (so answer 'a' is wrong).
Next in line 15 the main thread starts the thread t2 (created earlier in line 04 - lets call this thread 't1').
A short note: the start method doesn't cause thread t1 to execute, it's only changes a thread state from NEW to RUNNABLE.
At this point (after main thread executed line 15) we have two threads: main thread and t1 thread.
There are three possible scenarios here:
- JVM could continue executing the main thread, while t1 thread is still hold
- JVM could stop (hold) the main thread, and start running t1 thread
- JVM could execute both threads simultaneously
We cannot predict what scenario JVM will chose, the only thing we know is that the 't1' thread will print 21 at line 16,
then will join the main thread and will sleep until the main thread will finish.
At the same time the main thread will print 24 in line 16, then will return to the main() method.
But we cannot predict the order in which these numbers: 21 and 24 will be printed, so answer 'c' is wrong.
What we know for sure at this point is that the thread 't1' will continue after join() not earlier than the main thread will finish,
so the line 11 (which will print 22) will be executed after the main thread will end.
Now the main thread will call createThread(10, t1 )) method.
Next, the main thread in line 04 will create a new thread object (lets call this thread 't2'),
then will print 13 in line 14.
Next, the main thread will call start() method on thread 't2' - again, we don't know the order in which JVM will
execute these two threads, but we know for sure, that t2 thread will join the 't1' thread, and t2 thread will execute line 11
after 't1' will finish.
So, the joining path will be: main -> t1 -> t2. T2 will wait at line 08 until t1 will finish, and t1 will wait unti main will finish.
So the last number printed must be 12 printed in line 11 by t2 thread -> answer d is true.
Now we must decide which answer is true from the remaining two: 'b' and 'c' .
Answer 'c' cannot be true, because we cannot predict the order in which JVM will execute the main thread (which will print 24)
and the thread 't1' (which will print 21 in line 06) - answer c is wrong.
Number 22 from answer b will be printed by the thread 't1' in line 11 - this line will be executed after returning from join (this join will wait until the main thread will finish). Number 14 will be printed by the main thread in line 16,
so 14 will be printed before 22 and answer b is correct.
Finally: correct answers are: b and d
Hope this explanation is clear enough, and sorry for my terrible english
subject: Can some one explain in detail what actually is happening at Stack level on this set of code.