• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Threads and join()

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 -> https://coderanch.com/forums/posts/list/517455#2344028
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 -> https://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....
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Imad, why didn't main thread stop after executing the join() method, thats my doubt actually....
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mine might be a bad question but don't we invoke join() on some object ?
over here how is it able to know that with which thread it has to join with ..
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dharmesh Tyagi wrote:mine might be a bad question but don't we invoke join() on some object ?
over here how is it able to know that with which thread it has to join with ..



In this case, the join() method being referred to here, is an instance method of the java.lang.Thread class (and not just any "some object"). And of course, that class is used to represent a thread. When a thread calls the join() method, that thread waits for the completion of the thread that is represented by that Thread class instance.

Henry
 
Dharmesh Tyagi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
agree with you, but in such a case isn't it supposed to join with itself and thus a never ending process ?
my main doubt is how is it getting to know that it has to wait for thread created in constructor. because we are nowhere mentioning that thread .
 
reply
    Bookmark Topic Watch Topic
  • New Topic