Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Threads and 1.4 exam

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Do we need to know Thread.join() for the 1.4 exam?
Thanks.
Mansi
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming so. I read from the feedback from those who took 1.4 that test included 8-10 heavy-duty thread questions....
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's not much to know about join. It blocks waiting for the thread you're joining to finish running.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This program shows the working of the ThreadObject.join() method. The join() method forces a thread to wait for another thread to die before it is allowed to continue executing. Or, put in another way, thread A may call thread B’s join() method if A wants to stop executing and wait for thread B to finish before A continues.
Without t.join() – notice that t’s count is after main’s begin and end. This is because both the main and t threads execute independent of each other and since t sleeps, the main thread has plenty of CPU time to finish executing.

With t.join() - The output shows that T’s counting activity is between main’s begin and end. Now main waits for t to die 1st before continuing – because main calls t.join(). By calling this method, main tells the t thread that “I will join after you !”

Some extra experimenting :
Call t.join() before calling t.start() – This means main is telling t that it wants to join at the end of t’s execution EVEN BEFORE t STARTED EXECUTING. t.join() will have no effect on main. Maybe t replies : “you may continue since I have not even started executing… do not wait for me.” Join only makes main wait if t is already started.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!