Win a copy of Svelte and Sapper in Action this week in the 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Joining ThreadB to ThreadA

 
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: 4107
72
Hibernate Fedora Chrome
  • 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.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
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