This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Changing a variable inside a thread (and accessing it from the outside) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Changing a variable inside a thread (and accessing it from the outside)" Watch "Changing a variable inside a thread (and accessing it from the outside)" New topic
Author

Changing a variable inside a thread (and accessing it from the outside)

Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
How would you generally manage to change a variable value inside a thread and access the new value from the outside while the thread is running?

I passed an object to the thread's constructor and that object holds the value. The thread changes the value in it's run() method. The class that started the thread holds a reference to that object and thus can see the new value.

But this somehow seem's wrong to me. How would you achieve it?

A thread with a return value (using Future) is not an option since I need the updated value periodically while the thread is running.

Also, how do I properly terminate a thread? Do I just need to call interrupt()? That's all?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

I don't see the problem. To change the variable you simply assign a value to it. Using the = operator of course. To access it you simply get its value, again using the = operator but this time putting the variable on the right-hand side. Like this:


All of this is just basic Java -- which you already know, of course. But the fact that this variable is "inside" a thread is irrelevant. It just has to be accessible to the code which wants to assign a value to it or to access its value.

You're also asking about objects, which aren't the same thing as variables. But I can't quite tell what your problem is there. Again, if you have a reference to an object then you can work with the object; it doesn't matter that other threads might also have references to the object and be working with it.

As for ending a thread, the way to do that is for its run() method to return. If you want some other code outside the thread to cause it to end, then that code would have to do something to cause its run() method to return. I normally do this with one of those variables which you were asking about. The code inside the thread periodically checks to see if the "pleaseStop" boolean variable is true, and returns from run() if it is. The code outside the thread sets the "pleaseStop" variable to true.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 182
    
    2

I use getters and setters for this.

The get method does not have to anything special unless you want to make sure the value doesn't change while the caller is using it.

The trick comes in when you want to modify the same variable in different threads. Then the setter function should be synchronized.

There is no reason why methods in a single object can't be called from multiple threads.

In general it's bad form to make the variables public if they are accessed from multiple threads. It's too hard to control synchronization IMHO.

This thread would probably do a little better in the "Threads and Synchronization" forum.

What are you trying to do? We might be able to get more specific if we know more.


It's not what your program can do, it's what your users do with the program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing a variable inside a thread (and accessing it from the outside)
 
Similar Threads
Can a final variable could be reasign a new value?
Moon Lander
The output of the following programs
Not able to return the method value
Accessing a Runnable Interface method from its thread?