aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads and join() 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 and join()" Watch "Threads and join()" New topic
Author

Threads and join()

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181


Why the output of this programming coming out as 9? Why didn't the join() on line#18 caused the currently executing thread to join itself.?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Why didn't the join() on line#18 caused the currently executing thread to join itself.?


A thread joining itself would mean that it would have to wait until it has finished executing. That is a paradox. You can join one thread with another one. That makes more sense


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Hello Deepak, this is what I have discussed few days back..this
And came to the conclusion that using only join() causes currently executing thread to join itself, until it finishes its own execution. Now what do you say..?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

swaraj gupta wrote:Hello Deepak, this is what I have discussed few days back..this
And came to the conclusion that using only join() causes currently executing thread to join itself, until it finishes its own execution. Now what do you say..?


I would say Ankit's comment here, reflects my own -> http://www.coderanch.com/forums/posts/list/517455#2344028
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Deepak Bala wrote:
swaraj gupta wrote:Hello Deepak, this is what I have discussed few days back..this
And came to the conclusion that using only join() causes currently executing thread to join itself, until it finishes its own execution. Now what do you say..?


I would say Ankit's comment here, reflects my own -> http://www.coderanch.com/forums/posts/list/517455#2344028


"call to join() will never complete". Implies statements after join() shouldn't be executed then there shouldn't be any output....
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Dear Swaraj, below your code after inserting some SOP statments, as you see from the output there is actually 2 threads, the main() thread and Thread-0. Thread-0 will join and stuck there and the main thread will complete the job.
Output:
main in Main()
main before join()
Thread-0 in run()
main after join()
9 This is done by : main

Code:


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

In which thread is the show method called?? I believe its the main thread not the new thread created in the code, thus main is waiting for the other thread to complete...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Ankit Garg wrote:In which thread is the show method called?? I believe its the main thread not the new thread created in the code, thus main is waiting for the other thread to complete...

when we write, t.join() it causes currently executing thread to wait for thread t to finish its execution. Here in this case we have join() only(no other thread instance specified) then why didn't the main thread wait for itself to die....?
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Thanks for the enlightment Ankit, Yes the main thread will run and create the Strater object which will start another thread (Starter Thread) due to the start() in its constructor, the (Strater Thread) will not invoke show() in the Starter object [because show() is not inside run()] the main thread will do that, saying so, the main thread will join() the (Starter Thread) till it finish its job which is multiplying x by 2 and printing the SOP, and then main will complete its job.
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Hello Imad, why didn't main thread stop after executing the join() method, thats my doubt actually....
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Please let me know, what exactly only "join()" means when it is not invoked by a thread instance.

The description given in (KS && BB) and (khalid A.Mughal) is not turning to be insufficient for me.
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
swaraj gupta wrote:Hello Imad, why didn't main thread stop after executing the join() method, thats my doubt actually....


Dear Swaraj as I said(the (Strater Thread) will not invoke show() in the Starter object [because show() is not inside run()] the main thread will do that) and the main thread will do it by invoking {StarterObject.show()}, but actually the StarterObject is a Thread and hence your passing a refrence of this StarterObject to invoke join as
StarterObject.join();
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
swaraj gupta wrote:Please let me know, what exactly only "join()" means when it is not invoked by a thread instance.

The description given in (KS && BB) and (khalid A.Mughal) is not turning to be insufficient for me.


You are right swaraj .... I think the missunderstanding comes from thinking that the main thread and the Starter Thread are two copies of one type {Starter}, but I think this is wrong ... both are sure of type Thread, but the main thread is started by the JVM and I don't think its a Starter (its only an exceuter of the Starter).
so when you write join() only inside a class you actually mean this.join(), passing a refrence of the Starter Object to invoke join() and as in the code of the main() method this refrence is created by new Starter() which is of type Starter which is extends Thread
thats what i understand I hope its right

regards
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Imad Aydarooos wrote:Thanks for the enlightment Ankit

Actually my reply was not for you, your explanation was correct. Actually I didn't see your reply when I wrote my reply.

but the main thread is started by the JVM and I don't think its a Starter

Correct. The main thread is not of type Starter, it only calls the main method in Starter class...
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Dear Ankit, I thanked you because what you write shows to me the below in BOLD wrong sentence, actually the Starter Thread will not block, it will run and finish its job ... thanks again
Imad Aydarooos wrote:Dear Swaraj, below your code after inserting some SOP statments, as you see from the output there is actually 2 threads, the main() thread and Thread-0. Thread-0 will join and stuck there and the main thread will complete the job.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Ohh so your explanation was not right I saw the code and its output which was correct so I thought your explanation was also correct...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

I believe its the main thread not the new thread created in the code, thus main is waiting for the other thread to complete...


True.

what exactly only "join()" means when it is not invoked by a thread instance.


You are probably confused by the illusion that this problem has only one thread. Try printing the thread names to get more clarity.
nikita Gupta
Greenhorn

Joined: Sep 19, 2013
Posts: 2
public class Starter extends Thread
{
private int x;
public static void main(String args[]) throws Exception
{
new Starter().make();

}

public Starter()
{
x=5;

start();
}


public void make() throws Exception
{
join();
x=x-1;
System.out.println(x);
}

public void run()
{
x*=2;

}
}
in this question i want to know that there are only 2 threads main thread and thread 0 and the order of execution is-


1. The constructor sets x = 5.
2. The constructor calls start which in turn calls run in a new thread.
3. run executes x *= 5, making x equal to 10.
4makeItSo is called. It calls join, thus waiting for run to end.
and now here i want to know that in make method why value of x as 10 is used? the value 10 which is obtained in run method is local to the run method? then how value 10 is used by make method and also make method is invoked on main thread not on thread 0 then how it knows about value of x=10 for execution of x-1 and thus finally making it as 9? please correct me where i am wrong..according to me after run method when make method is executed it should use value of x=5 for execution of x-1 and thus finally printing 4 as answer..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Threads and join()