Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread doubt please help

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karu,
I had posted a same question some time back. Had got an answer - could you search up for that post.

--
Shivani.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks weber a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic