• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Join method  RSS feed

 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can someone explain me the output of the following code.
Please explain how the code works to ensure the output behaviour...
I 've tried hours to understand it but I cannot.

The following code produces this output always :
A.The number 14 is printed before number 22
B.The last number printed is 12.

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
krishna valavala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kitty Creation,

I will try to explain the reasoning behind
A) 14 printing before 22 and
B) Last number printed is 12

Here we have 3 threads in play.

1. Main Thread, Call it MT
2. Thread T2, Created in the create method, for the first time
3. Thread T22, Created in the create method, second time.

Note that create method is called twice by the main thread MT. For the first time, create is
called with integer argument 20 and second time around it is called with integer argument 10.

Now, when create is called the first time, MT creates T2 instance, prints 23, starts T2.

If for some reason main thread is taken out, (If it is blocked) T2 gets a chance to run.
Other wise Mt prints 24 and returns to call another the second create.

Now if the main thread gets blocked, T2 gets a chance to run. But since there is no reason
to pre-empt the main thread, the second create is called, which creates T22 instance, prints
13, and start T22.

Again if Main thread is not blocked, it prints 14 and returns. If it is blocked, it will not print 14.
Now depending on the scheduler either T2 or T22 gets a chance to run.

If T2 runs, 21 is printed and T2 waits on main thread (MT) to finish, MT will print 14 (if not
printed earlier), and then T2 prints 22. Then T22 runs and prints 11 and 12

If T22 runs it prints 11 and waits on T2 to finish. T2 runs and prints 21 and waits for main
thread to finish. The main thread prints 14, if not printed earlier. Then T2 prints 22.
T22 prints 12 and then finishes.

So you see always 14 is printed by the main thread(MT) before 22 is printed by the T2 and also how
T22 prints 12 at the end.

I hope I explained clear enough
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishna valavala - Thanks for the detailed reply.
However my understanding is still not clear.

Plese elaborate this statement :

"But since there is no reason to pre-empt the main thread, the second create is called "



I've understood till the point main thread calls T2 thread
After t2.start() is called for thread T2 the following also can get executed :
23 (printed by MT)
21 (printed by T2)
---T2(waits for main to finish)--
24 (printed by MT)
22 (printed by T2)

similary for second call by MT.

13 (printed by MT)
11 (printed by T22)
---T22(waits for main to finish)--
14 (printed by MT)
12 (printed by T22)


========== Wats wrong with the above flow - will this path of execution be ever taken ?

Thanks for replying once more...

Regards,
Kitty
 
krishna valavala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kitty Creation:
To explain the quote
"But since there is no reason to pre-empt the main thread, the second create is called "
let me dwell into a bit of details about the java thread scheduler.

In Java there are two reason a thread can be moved out of the run method by the thread scheduler.
1. Higher priority thread becomes runnable. (Pre-emptive scheduling)
2. Allocated time is expired (Time Sliced scheduling)
In our case since we are not assigining any priority, all the threads run with the same priority.

Even on time-sliced scheduling systems, my argument will hold good.

Now let us look at your log of events.

23 (printed by MT) This will happen.
21 (printed by T2) This can happen, if the MT leaves control of the cpu. So immediately after t2.start() if you have Thread.yield() or Thread.sleep(100) 21 will be printed.
---T2(waits for main to finish)--
24 (printed by MT) This will happen
22 (printed by T2) This will not happen until MT is done because of "---T2(waits for main to finish)--"
similary for second call by MT.
13 (printed by MT) This will happen
11 (printed by T22) This will happen
---T22(waits for main to finish)--
14 (printed by MT) This will happen
22 (Printed because MT is done)
12 (printed by T22) This will happen

Krishna Valavala
 
R Sawant
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, what the "ArrayList alr = new ArrayList()" is doing there ?

Ramdas
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!