This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question" Watch "Thread question" New topic
Author

Thread question

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477

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?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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

Joined: Apr 25, 2005
Posts: 809
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

Joined: Jan 05, 2006
Posts: 477
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

Joined: Apr 25, 2005
Posts: 809
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

Joined: Jan 05, 2006
Posts: 477
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?
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
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


SCJP 5
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question
 
Similar Threads
Thread doubt please help
Joining threads
threads
join
Threading related question from khalid mughal's book