aspose file tools*
The moose likes Threads and Synchronization and the fly likes How do I pass a update a variable to all running Threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How do I pass a update a variable to all running Threads?" Watch "How do I pass a update a variable to all running Threads?" New topic
Author

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

Daimon Masaru
Greenhorn

Joined: Oct 13, 2008
Posts: 5
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?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Daimon Masaru
Greenhorn

Joined: Oct 13, 2008
Posts: 5
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
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

Joined: Dec 08, 2010
Posts: 1506
    
    5

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 08, 2010
Posts: 1506
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I pass a update a variable to all running Threads?