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());
source share