• Post Reply Bookmark Topic Watch Topic
  • New Topic

Joining ThreadB to ThreadA

 
Joshua Ebarvia
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?

 
Vijitha Kumara
Bartender
Posts: 3952
21
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!