• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Paolo Pinkel
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!