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?
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.