相关疑难解决方法(0)

是否可以修改非易失性变量,以便另一个线程能够"看到"更新?

我有一个Thread-X,它每秒读取一个非易失性变量,这样做没有任何同步手段.

现在我想知道是否有某种方法可以修改Thread-Y上的非易失性变量,以便Thread-Y的写入(最终)在Thread-X上可见?

public class Test {
    private static boolean double = 1; // this variable is
                                             // strictly not volatile

    public static void main(String args[]) {
        new java.lang.Thread(new java.lang.Runnable() {

            @Override
            public void run() {
                while (true) {
                    System.out.println(variable);
                    try {
                        java.lang.Thread.currentThread().sleep(1000);
                    } catch (java.lang.InterruptedException e) {
                    }
                }
            }
        }).start(); // line 17
        new java.lang.Thread(new java.lang.Runnable() {

            @Override
            public void run() {
                // the task is to change variable to "2" such the write
                // is (eventually) …
Run Code Online (Sandbox Code Playgroud)

java synchronization

4
推荐指数
1
解决办法
1626
查看次数

标签 统计

java ×1

synchronization ×1