从Runnable类中访问共享变量 [英] accessing shared variable from inside a Runnable class

查看:414
本文介绍了从Runnable类中访问共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Main类的main()方法中定义一个共享变量。我需要两个线程才能访问该共享变量。我通过实现Runnable接口并实现接口的抽象run()方法来创建线程。我如何从我的类中定义的实现Runnable接口的run()方法中引用Main类的main()方法中定义的共享变量?显然只是通过名字调用它们不起作用 - 因为它们出现在我的Runnable类的范围之外。

I need to define a shared variable in my Main class's main() method. I need two threads to be able to access that shared variable. Im creating the threads by implementing the Runnable interface and implementing the abstract run() method of the interface. How do i refer to the shared variable defined in the Main class's main() method from within the run() method defined in my class that implements the Runnable interface? Obviously just calling them by name is not working - as they appear out of my Runnable class's scope.

编辑 - 道歉,这是一个简单的例子

EDIT - apologies, here is a simple example

public Class DoThread implements Runnable {

    public void run(){
        sharedVar += 1
    }


}

和单独的.class文件:

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

    }


}

我应该创建一个对象来存储共享var,然后在创建线程时将该对象传递给DoThread()构造函数?有时在java通过ref或传递var之间会感到困惑,所以如果我这样做,t2会看到t1对共享var的改变吗?

so should i be creating an object to store the shared var in and then passing that object to both the DoThread() constructors when creating the threads? I get confused sometimes between when java passes by ref or passes by var, so if i do it this way, will changes to the shared var by t1 be seen by t2?

推荐答案

好吧,如果你声明一个本地变量,除了在该方法中创建的类之外,你将无法引用它。

Well, if you declare a local variable you won't be able to refer to that anywhere other than in classes created within that method.

你在哪里实施 Runnable ?如果它在同一个类中,那么你可以使它成为一个实例变量并使 main 在你创建线程的同一个实例上设置变量,或者make它是一个静态变量。如果 Runnable 是在另一个类中实现的,那么在构造该类的实例时,您可以为其提供所需的数据 - 目前还不清楚这意味着什么......正如其他人所说,代码会很有用。 (例如,线程是否需要能够在原始数据中看到更改?)

Where are you implementing Runnable? If it's in the same class, then you could either make it an instance variable and make main set the variable on the same instance that you're creating the thread from, or make it a static variable. If Runnable is implemented in a different class, then when you construct an instance of that class you could give it the data it needs - it's not clear exactly what that means at this point... As others have said, code would be useful. (For example, do the threads need to be able to see changes in the original data?)

顺便说一下,线程相对先进而在类之间传播数据是相对基本的。如果您是Java新手,我建议您开始使用比线程更简单的东西。

As an aside, threading is relatively advanced, whereas propagating data between classes is relatively basic. If you're new to Java, I'd recommend getting started on easier things than threading.

编辑:对于您的示例,您应该使用 AtomicInteger ,像这样:

For your example, you should use an AtomicInteger, like this:

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
    }
}

这篇关于从Runnable类中访问共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆