Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Joining ThreadB to ThreadA  RSS feed

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently studying the Thread.join() method, and I'm not sure If I'm getting it right.

Here is what I want. I have two objects which implements Runnable, ThreadA and ThreadB

ThreadA has a static variable int x. This variable is given a value of 5 if the ThreadA is started.

ThreadB displays the value of the static variable of ThreadA. But I want it to print the value of 5 instead of 0(default value).

What I did was create an instance of ThreadA inside the run method of ThreadB and started it. After the .start(), I called method
join() so that ThreadB will only run after ThreadA dies. In which case, it has already assigned the value of 5 to int x.

So what I want is , ThreadB should ONLY run after ThreadA is DONE.

Is my understanding about join() right? Does my code below support my understanding? Is it the right code to demonstrate what I want?
Is it the poper way of doing the "ThreadB should only run after ThreadA is done"?

I have coded it below.



after running the code. Here is the result


The output is right as expected, but did I do it the right way/proper way?

 
Bartender
Posts: 4048
48
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is my understanding about join() right? Does my code below support my understanding? Is it the right code to demonstrate what I want?



Yes. That's correct according to your requirement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!