File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread doubt please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread doubt please help " Watch "Thread doubt please help " New topic
Author

Thread doubt please help

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
public class Joining {
static Thread createThread(final int i, final Thread t1) {
Thread t2 = new Thread() {
public void run() {
System.out.println(i+1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i+2);
}
};
System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}

public static void main(String[] args) {
createThread(10, createThread(20, Thread.currentThread()));
}
}

I executed the above program and got output as
---------- Java ----------
23
24
13
14
21
22
11
12
----------------------
But i am not understanding the code ?

please help me
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Karu,
I had posted a same question some time back. Had got an answer - could you search up for that post.

--
Shivani.


/** Code speaks louder than words */
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Try these...

http://www.coderanch.com/t/245443/java-programmer-SCJP/certification/join-threads

http://www.coderanch.com/t/243927/java-programmer-SCJP/certification/join

http://www.coderanch.com/t/246361/java-programmer-SCJP/certification/Help-Required-code-threads


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
First, take the arguments to the createThread method:

1. The first time this method is called, the Thread argument that is passed to it is the mainthread.
2. The second time this method is called, the Thread argument that is passed is the Thread that was created in the first call.

Now, consider the thread stack:

1. The thread t2 that is created in the first call joins Thread t1 which is the mainthread.
2. The thread t2 that is created in the second call joins Thread t1, again, but this time this is the Thread created in the first call.

Let's give each thread a name. main thread, thread-first-call, thread-second-call.

This is the Thread Stack:

___________________________
|mainthread (i=20,i=10) |
|_________________________|
|thread-first-call (i=20) |
|_________________________|
|thread-second-call (i=10)|
|_________________________|

Top-down is the order in which the threads are eexecuted. thread-first-call will be executed after main thread finishes. thread-second-call will be executed after thread-first-call finishes.

Each thread has its own copy of i (indicated in brackets). mainthread executes and prints the value of (i+3) and (i+4) .. i.e 23 and 24 the first time. main thread executes again and prints the value of (i+3) and (i+4) .. i.e. 13 and 14. thread-first-call executes and prints the value of (i+1) and (i+2) .. 21 and 22. Last, thread-second-call executes and prints (i+1) and (i+2) .. i.e. 11 and 12.

NOTE:
1. To understand more clearly, print the give all threads a name and print them out along with (i+n)
2. Hope you got that run() method for the Thread t2 is defined in an anonymous inner class.


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
Thanks a lot to all of you .

do we except such type of questions in exam.

i am grate to all of you .

i appreciate you all for taking time to explian me .

I have one doubt
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Karthik Rajashekaran:
...do we except such type of questions in exam...

This is definitely more complex than what you should expect on the actual exam. It's good practice, because if you can work through this and understand what's going on, you're developing skills that will definitely help you on the exam. But you shouldn't need to spend this much time on a real question.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
thanks weber a lot
 
 
subject: Thread doubt please help
 
Similar Threads
Joining Thread, Explanation required
threads
A hard question
Help Required with this code on threads
Thread question