GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads 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 "threads" Watch "threads" New topic
Author

threads

sandeep dhingra
Ranch Hand

Joined: Jul 30, 2005
Posts: 41
can some one please explain the output and how the join method works in this example.....actually the whole program....

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()));
}
}



output is
23
24
13
14
21
22
11
12
Amieya Prabhaker
Ranch Hand

Joined: Apr 23, 2006
Posts: 45
Wonder if posting some debug print messages and also a Thread.activeCount() helps in understanding the flow of the program.

Here's the modified code & a sample output.
Please be aware that there may be other outputs that are not predictable.
===========================================================================
public class joining{
static Thread createThread(final int i,final Thread t1){
Thread t2=new Thread(){
public void run(){
System.out.println(i+1);
System.out.println("Inside anonymous run "+ this.getName());
try{
t1.join();

System.out.println("After join:Active Count " + Thread.activeCount());
}
catch(InterruptedException e){}
System.out.println(i+2);
System.out.println("After run "+ this.getName());
}
};
System.out.println(i+3);
System.out.println("After thread declaration Active Count "+ Thread.activeCount());
t2.start();
System.out.println(i+4);
System.out.println("Before Return Active Count " + Thread.activeCount());
return t2;
}


public static void main(String[] args){
createThread(10,createThread(20,Thread.currentThread()));
System.out.println("Active Count in main" + Thread.activeCount());
}
}

==========================================================================
TWO SAMPLE OUTPUTS:-
==================

C:\Amieya\CODES\j2ee>java joining
23
After thread declaration 1
24
Before Return Active Count 2
13
After thread declaration 2
14
Before Return Active Count 3
Active Count in main3
21
Inside anonymous run Thread-0
After join:Active Count 3
22
After run Thread-0
11
Inside anonymous run Thread-1
After join:Active Count 2
12
After run Thread-1

C:\Amieya\CODES\j2ee>java joining
23
After thread declaration 1
24
Before Return Active Count 2
13
After thread declaration 2
14
Before Return Active Count 3
21
11
Inside anonymous run Thread-0
Inside anonymous run Thread-1
Active Count in main3
After join:Active Count 2
22
After run Thread-0
After join:Active Count 1
12
After run Thread-1

C:\Amieya\CODES\j2ee>
Amieya Prabhaker
Ranch Hand

Joined: Apr 23, 2006
Posts: 45
Before I try an explanation preferably indent your code to understand program flow. (Java gurus here, please let me know if I am correct in my attempt!),

Few concepts:
============
- 3 threads here on the stack- main, 2nd thread created using createThread(20,Thread.currentThread()) & the 3rd thread created using createThread(10,2nd thread).

- Program calls createThread(20,Thread.currentThread()) and flow rolls to
System.out.println(i+3)- first output 23. When start is called in next line thread count has increased to 2. From hereon, the 2 bounce back and forth. until within run, there is a join, which essentially says, quoting K&S- "Join me (the current thread) to the end of t1, so that t1 must finish before I (the current thread) can run again."

- Now before join of 2nd thread is called there is a possibility, main goes ahead n creates thread 3. There is a possibility thread3 run join may be called first (I think!?) So main may join to either of the threads.(??) Once the run is over, with further bouncing the next thread join is called, until

- Finally after the 2 threads have run, main exits.
Amieya Prabhaker
Ranch Hand

Joined: Apr 23, 2006
Posts: 45
Can anyone confirm the above "hypothesis"? Must admit this question was among the tricky thread questions I've seen on this forum.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

System.out.println(i+3);
t2.start();
//If the OS switchs t2 to run before call this one.
System.out.println(i+4);

You may get result like:
23
21
24
13
11
14
22
12
sandeep dhingra
Ranch Hand

Joined: Jul 30, 2005
Posts: 41
i am still not able to understand because u have used the word possibility many times and the output is the same everytime the prog. is run.
can u plz tell me which is thread t1 and t2. when we write t1.join which code gets executed. is t1.join being called two times. if so howww........plz help me this is rotating my head....
 
GeeCON Prague 2014
 
subject: threads