从java中的另一个线程访问线程的变量

The*_*iot 9 java setter multithreading decrement

我试图在java中的另一个线程中访问和修改线程的变量,我真的不知道如何做到这一点.

例如:

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();
Run Code Online (Sandbox Code Playgroud)

谢谢你,如果你有任何想法!

有没有办法在java中为线程创建一个getter和setter?

编辑:答案很好,但我不清楚我的问题,我会尝试提出一个更好的问题

Wil*_*son 5

创建一个 runnable,并使用您在所述 runnable 中定义的 setter 和 getter。

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(){}
}
Run Code Online (Sandbox Code Playgroud)

volatile此处使用了Note关键字。volatile 关键字确保如果此字符串在一个线程中发生更改,则所有线程都会看到更改。相反,如果我确保对 String 对象的唯一访问是通过同步上下文,那么 volatile 关键字将是不必要的。

为了证明我的观点,上面的代码和下面的代码都是线程安全的,但是是不同的,因为没有两个线程可以进入setString,并getString在下面的例子中同时进行。

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(){}
}
Run Code Online (Sandbox Code Playgroud)

一个线程实际上只是执行一个可运行的。你可以这样使用它:

MyRunnable runnable = new MyRunnable();
Thread myThread = new Thread(runnable);
myThread.start();
String myString = runnable.getString();
Run Code Online (Sandbox Code Playgroud)

对原语使用原子值很好,但是如果您想共享更复杂的对象,则必须阅读有关线程和同步的内容。

例如:

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());
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码显示了通过synchronized关键字与非原始对象同步的示例。在方法定义中使用 synchronized 关键字会锁定使用自身作为同步对象的类。

最后要注意的是,synchronized 关键字不仅仅用于方法定义中。您可以使用它来同步方法中的实例,就像我runStatRunnable.


Pet*_*rey 5

您可以使其工作,但我建议您使用在线程之间共享的AtomicInteger.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用AtomicReference来引用对象.