Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

join()

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we call join method on a thread instance.


this means join the currently running thread to the end of thread t.

But
if it is in a method as shown in the above code...

What it means??? actually it is showing compiler error for handling the Exception. But I want to know about join();
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Banu Chowdary wrote:we call join method on a thread instance.


What it means??? actually it is showing compiler error for handling the Exception. But I want to know about join();



you just wrap the join() method with the try catch block since the mehtod signature demands it.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ,I am not asking about compiler error.

I have seen

And I know the meaning of it..

But just means to the end of which thread we are connecting our currently running thread???
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here it is like:



So it is trying to join to itself, trying to wait for itself to finish, that will not happen ever.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Singh wrote:So it is trying to join to itself, trying to wait for itself to finish, that will not happen ever.


If it is trying to wait for itself... then why is the for loop executing???
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B page 699 figure 9-3 has an example which shows the output of t.join(). I think it will help you understand a lot.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think calling this.join() will not affect current thread, it will not wait for itself, it will resume normally, I am assuming this from the output.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnot understand....
Can you explain a bit clearly....
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t.join() means to join the calling Thread (in this case t) to the end of the Currently running thread

so if you start a thread in main (dont forget main starts a new thread of execution (stack) and call join from another thread instance (for example t) then t will be joined to the end of main, (i.e run when main is completed):



this means that newThread will always run once main is completed:

And the output is...



 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Singh wrote:I think calling this.join() will not affect current thread, it will not wait for itself, it will resume normally, I am assuming this from the output.




To elaborate some more.... Even though MyThread1 is a thread object, it is *not* being used as one in this example. It is being used as a runnable object for another thread object -- and it is that thread object that is being started. So, when you call the join() method on the MyThread1 object, you are trying to join a thread that was never started, hence, it will return immediately.

Henry
 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

t.join() means to join the calling Thread (in this case t) to the end of the Currently running thread

so if you start a thread in main (dont forget main starts a new thread of execution (stack) and call join from another thread instance (for example t) then t will be joined to the end of main, (i.e run when main is completed):



Sorry, but this is not true... in your example, the reason the main thread's output came first is simply because it came first. If you had placed a sleep() method call in the main thread before the printout, the other thread would have printed first.

Henry
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DOH!

Mr W, you are right, and I should have known that! yes the join() method adds the currently running thread to the end of the Thread calling join(). With your sleep() suggestion I know get:



Shoot me now So as I now understand it:


is telling main to go from running to runnable until myThread is run, at which time main becomes runnable once more (and most likely runs).

My Head is like the JVM Scheduler at times, unpredictable. :

Many Thanks for the correction
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephen Davies wrote:Shoot me now



As you wish
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic