• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread question

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

Output:
23
24
13
14
21
22
11
12

Can anyone please explain the program output step by step execution for me? I got how to get the value 24.How is the value 13 is coming?

First, the inner createThread(20, Thread.currentThread()) is executing,so the creatThread method invocation gives thread t2 is in new state(anonymous inner class),t1.join() is executed.
System.out.println(i+3);-------------for this line 23 is printed.
T2.start() will start the t2 thread.so go to execute the run() method. Within that method, (i+1)24 is printed.Then I don�t know how the execution goes?Help me?
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:
System.out.println(i+3);-------------for this line 23 is printed.
T2.start() will start the t2 thread.so go to execute the run() method. Within that method, (i+1)24 is printed.Then I don�t know how the execution goes?Help me?



No

Remarks which you have made is not correct. Output 24 is not because of call to run method rather its because of SOP just before return statement. Remember i is not updated. Secondly run() method will not be started instantaneously as start() gets called. You never know when run() will execute. If run() method gets executed instantaneously, then you will get output 21 not 24 after 23.

Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naseem for the clarification.For the (i+3) 23 is printing,t2.start() is not started, (i+4) 24 is printing.Please help me understasnding how this works further.Actually,I am nervous to attend any thread question.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After outputing 23 and 24, t2 is returned and now inner createThread will point to t2.

Then after that Outer createThread method gets executed prints 13 (10+3) and 14. After that first thread executes run() method, prints 21 (20+1) and then waits for the main method to finish. When main thread dies, it returns from join() prints 22 (20+2). Similary second thread executes run(), prints 11, then waits for the first thread to die. When first finishes, it returns and prints 12.

Naseem
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Naseem.I was trying to understand join() and I have a doubt.
The output of the above program is

//inner createThread
createThread(20, Thread.currentThread();-----for this,(20+3) 23 is printed
(20+4) 24 is printed
T2 is returned
//outer createThread
createThread(10,t2)-------------------for this(10+3) 13 is printed
(10+4)14 is printed
T2 is returned
// calls the run() method------(20+1) 21 is printed
T1.join()--------Here is my doubt starts. Main thread join to the end of t1.until we finish executing t1,main thread will be placed in blocking state.How come that t1 thread needs to wait until we finish executing main method? Please help me as i am really confused.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
join() method waits for this thread to die.

See join() method is called on two threads...

1. main thread called by first annonymous thread

It means first annonymous thread will wait for main thread to die.

Second time join() is called on first annonymous thread and called by second annonymous thread. In this case, second thread will wait for first to die.


Any doubt in that?

One more thing, I added one SOP(t1) line before call to join(), just to see the thread name.

Here are some outputs:






Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Naseem.I got it.But���
When I print the thread name I got



It started from Thread-0 not from main.Why?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naseem Khan:
After outputing 23 and 24, t2 is returned and now inner createThread will point to t2.
Then after that Outer createThread method gets executed prints 13 (10+3) and 14.
Naseem



Hi Naseem,

I know the output is correct if we run it.

How do you know for sure first thread won't print 21 first then 13 and 14??

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

Originally posted by Shiva Mohan:
Thank you very much Naseem.I got it.But���
When I print the thread name I got

code:
--------------------------------------------------------------------------------

23241321Thread[Thread-0,5,main]142211Thread[Thread-1,5,main]12

--------------------------------------------------------------------------------




It started from Thread-0 not from main.Why?



I think you wrote SOP(this);

It prints me main and Thread-0 with this line...



Originally posted by Anthony Karta:
Hi Naseem,

I know the output is correct if we run it.

How do you know for sure first thread won't print 21 first then 13 and 14??

thanks
anthony



Well its all about when first thread gets started, if it gets started before the return statement or even if it returns and main thread has not called the outer createThread method yet then you will see 21 before 13 and 14.


Naseem
reply
    Bookmark Topic Watch Topic
  • New Topic