• 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

threads

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone confirm the above "hypothesis"? Must admit this question was among the tricky thread questions I've seen on this forum.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic