• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I pass a update a variable to all running Threads?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I pass a update a variable to all running Threads?

I know I can pass variables to a Thread by making a class implement Runnable, making a constructor for the class, create an instance of the class to pass in the variables then start the Thread.

But how do I update a variable into a Thread that is already running and this updated variable must be visible to all the Threads that are running at that time?

I am thinking...



Yeah yeah, I know, I need to put in a Timer for the Thread to run for sometime too.

But, this way if I change the value of value in vTU during runtime, like maybe take in an input from the user at runtime and store it in vTU, will the the value in the vTU objects in both Threads change? Thus, changing the output?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daimon Masaru,

Welcome to CodeRanch!

Are you writing all your code in one file? In that case, it will throw a compile time error because all three classes are public.

Secondly, you are not creating threads, and you are calling start method on ThreadClass object (which is-a runnable, but not a thread), so there also you'll get a compilation error.

Apart from that, yes, if you pass same reference to two threads and you change values within it at any one thread, the value will be reflected in all other threads.

I hope this helps.
 
Daimon Masaru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:Hi Daimon Masaru,

Welcome to CodeRanch!

Are you writing all your code in one file? In that case, it will throw a compile time error because all three classes are public.

Secondly, you are not creating threads, and you are calling start method on ThreadClass object (which is-a runnable, but not a thread), so there also you'll get a compilation error.

Apart from that, yes, if you pass same reference to two threads and you change values within it at any one thread, the value will be reflected in all other threads.

I hope this helps.



Thank you. I typed the above code only as an example, so I didn't checked it through.

But yeah, that is what I need to know. Thank you very much.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do I pass a update a variable to all running Threads?



I don't understand this usage.

A Thread executes code, it doesn't normally carry variables around. The code may access a variable to get the current value.

To make a variable value accessible to all running Threads it just needs to be public or have a public access method in an object all the running Threads have access to.

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William Brogden,

I interpreted the question like : if two threads are dealing with same reference, and some change is made to that in one thread, is it reflected in other thread?

That's why I said yes - provided the thread also read corresponding values after the modification. But yes, if not passed specifically, variables have to be public.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:if two threads are dealing with same reference, and some change is made to that in one thread, is it reflected in other thread?



If you want the other threads to see it, then you need to declare the variable volatile, or synchronize all access to it, or use an appropriate java.util.concurrent.atomic.AtomicXxx object. If you don't do this, there's no guarantee of when or even if the other threads will see the new value.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:there's no guarantee of when or even if the other threads will see the new value


Yes. I missed this part. Thanks for mentioning.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic