Accessing a shared variable from the Runnable class

I need to define a shared variable in my main class main (). I need two threads to access this shared variable. Im creates threads by implementing the Runnable interface and implementing the abstract run () method of the interface. How can I refer to a shared variable defined in the main () method of the main class () from the run () method defined in my class that implements the Runnable interface? Obviously, just calling them by name does not work - as they come from my scope of the Runnable class.

EDIT - apologies, here is a simple example

public Class DoThread implements Runnable { public void run(){ sharedVar += 1 } } 

and in a separate .class file:

 public Class Main { public static void main(String[] args) { int sharedVar = 0; Thread t1 = new Thread(new DoThread()); Thread t2 = new Thread(new DoThread()); t1.start(); t2.start(); t1.join(); t2.join(); } } 

So should I create an object to store the general var in, and then pass this object as DoThread () constructors when creating the threads? I am sometimes confused between when java passes ref or passes var, so if I do it this way it will change to a common var on t1 after seeing t2?

+6
source share
4 answers

Well, if you declare a local variable, you will not be able to refer to it anywhere except for classes created as part of this method.

Where do you implement Runnable ? If it is in the same class, then you can either make it an instance variable, or make main set the variable in the same instance from which you create the stream, or make it a static variable. If Runnable implemented in another class, then when you create an instance of this class, you can provide it with the data it needs - it does not quite determine exactly what it means at the moment ... As others said, the code will be useful. (For example, so that threads should see changes in the source data?)

Aside, streams are relatively advanced, while the distribution of data between classes is relatively basic. If you are new to Java, I would recommend starting work on easier tasks than threading.

EDIT: for your example, you should use AtomicInteger , for example:

 import java.util.concurrent.atomic.AtomicInteger; class DoThread implements Runnable { private final AtomicInteger counter; DoThread(AtomicInteger counter) { this.counter = counter; } public void run() { counter.incrementAndGet(); } } public class Test { public static void main(String[] args) throws InterruptedException { AtomicInteger shared = new AtomicInteger(0); Thread t1 = new Thread(new DoThread(shared)); Thread t2 = new Thread(new DoThread(shared)); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(shared.get()); // Prints 2 } } 
+11
source

The code will be helpful.

If you use an anonymous class, you can declare the variables as final , and they will be available in the anonymous class.

 public void main(String[] args) { final int myvar = 0; new Thread(new Runnable() { public void run() { System.out.println("myvar: "+myvar); } }).start(); } 
+7
source

You need to pass a link to the common value. If this is no longer a link, you need to change it to AtomicReference, AtomicInt, etc. Or an array of the value you want to use.

+2
source

This is the place where PassByRef is more efficient. Pass the variable reference for both executable instances. Thats all ... from now on, it can be specified in your launch method.

Example:

Thread th1 = new thread (Myrunnable (varRef)). Similarly for another thread

0
source

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


All Articles