Accessing a thread variable from another thread in java

I am trying to access and change a thread variable in another thread in java and I really don't know how to do this.

ex:

Runnable r1 = new Runnable() { int value = 10; public void run() { // random stuff } } Runnable r2 = new Runnable() { public void run() { // of course the bellow line will not work r1.value--; // I want here to be able to decrement the variable "value" of r1 } } Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); 

thanks if you have any ideas!

Is there a way to create getter and setter for a stream in java?

EDIT: the answers were good, but in my question I was not clear, I will try to ask a better question

+6
source share
2 answers

You can do this work, but I suggest you use AtomicInteger , which is shared between threads.

 final AtomicInteger value = new AtomicInteger(10); Runnable r1 = new Runnable() { public void run() { // random stuff using value } } Runnable r2 = new Runnable() { public void run() { value.decrementAndGet(); } } 

You can use AtomicReference to refer to objects.

+4
source

Create a runnable and use the settings and getters that you define in the specified runnable.

 public class MyRunnable implements Runnable{ private volatile String myString; public String setString(String value){this.myString = value;} public String getString(){ return myString; } public void run(){} } 

The volatile keyword is used here. The volatile keyword ensures that this line changes in one thread so that all threads see the change. If instead I guarantee that the only access to the String object is through a synchronized context, then the volatile keyword is not required.

To demonstrate our point of view, the code above and the code below are thread safe, but different from each other, since none of the 2 threads can enter setString and getString at the same time in the example below.

 public class MyRunnable implements Runnable{ private String myString; public synchronized String setString(String value){this.myString = value;} public synchronized String getString(){ return myString; } public void run(){} } 

The thread really just runs runnable. You can use it like this:

 MyRunnable runnable = new MyRunnable(); Thread myThread = new Thread(runnable); myThread.start(); String myString = runnable.getString(); 

Using atomic values ​​for primitives is fine, but if you ever want to share a more complex object, you will need to read about thread and synchronization.

For instance:

 public class Stats{ int iterations; long runtime; public Stats(){ iterations = 0; runtime=0; } public synchronized void setIterations(int value){this.iterations = value;} public synchronized void setRuntime(long milliseconds){ this.runtime = milliseconds; } public synchronized int getIterations(){ return iterations; } public synchronized long getRuntime(){return runtime;} } public class StatRunnable implements Runnable{ Stats stats; boolean active; public StatRunnable(){ this.active=true; } public Stats getStats(){ return stats; } long calculateRuntime(){return 0L;} public void run(){ while(active){ //i'm synchronizing with stats to ensure no other thread alters values //simultaneously. synchronized(stats){ stats.setIterations(stats.getIterations()+1); stats.setRuntime(calculateRuntime()); } } } } 

This code shows an example of synchronization with non-primitive objects using the synchronized . Using the synchronized keyword in a method definition blocks the class, using itself as a synchronization object.

Finally, the synchronized keyword is used not only in method definitions. You can use it to synchronize instances in methods, as was done in the run method, in StatRunnable .

+4
source

Source: https://habr.com/ru/post/949868/


All Articles