java - Java中的线程结构

标签 java multithreading

我有几个关于 Java 线程的问题。这是代码:

TestingThread 类:

public class TestingThread implements Runnable {
    Thread t;
    volatile boolean pause = true;
    String msg;

    public TestingThread() {
        t = new Thread(this, "Testing thread");
    }


    public void run() {
        while (pause) {
            //wait
        }

        System.out.println(msg);
    }

    public boolean isPause() {
        return pause;
    }

    public void initMsg() {
        msg = "Thread death";
    }

    public void setPause(boolean pause) {
        this.pause = pause;
    }

    public void start() {
        t.start();
    }

}

和主线程类:

public class Main {
    public static void main(String[] args) {
        TestingThread testingThread = new TestingThread();
        testingThread.start();
        testingThread.initMsg();
        testingThread.setPause(false);


    }
}

问题列表:

  1. t 应该是 volatile 吗?
  2. msg 应该是 volatile 吗?
  3. setPause() 应该同步吗?
  4. 这是良好线程结构的一个很好的例子吗?

最佳答案

您的问题 2 已经触及了相当微妙的地方。

在您非常具体的情况下,您:

  1. 首先从主线程写入msg
  2. 然后从主线程写入 volatile 暂停
  3. 然后从子线程读取 volatile pause
  4. 然后从子线程读取msg

因此,您已经在 msg 的写入和读取之间建立了一个happens-before关系。因此,msg 本身不必是 volatile 的

然而,在现实生活中的代码中,您应该避免依赖这种微妙的行为:最好过度应用 volatile 并平静地 sleep 。

以下是 Java Language Specification 的一些相关引述:

  • If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).

  • If an action x synchronizes-with a following action y, then we also have hb(x, y).

请注意,在我的操作列表中,

  • 按照程序顺序,1 位于 2 之前;
  • 3 和 4 相同;
  • 2 与 3 同步

至于您的其他问题,

  • ad 1:t 不必是 volatile 的,因为它是在线程创建之前写入的,并且以后不会发生变化。启动线程会自行引发 happens-before
  • 广告 3:setPause 不必同步,因为它所做的只是设置 volatile 变量。

关于java - Java中的线程结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24823590/

相关文章:

Java程序使用简单循环查找整数数组的重复值

java - 谁可以将所有路径打印到txt文件中

java - 在多个EC2实例中远程执行程序并等待结果

python - 使用 ZeroMQ 实现多线程服务器

java - 在 ThreadPoolExecutor 中设置 allowCoreThreadTimeOut() 是一个好习惯吗?

c++ - 使用 OpenMP 线程重新初始化 vector 是否更快?

java - 当系统上安装多个 JRE 时,使用哪个 rt.jar?

java - Java中什么会更快 : DB2 INSERT/SELECT or File Write/Read

c - 使用多线程计算数据但并没有减少时间

java - BAD 请求 400 使用多部分/表单数据泽西客户端响应 java