【发布时间】:2019-12-01 08:04:18
【问题描述】:
调试结果为 77777,否则为 97777。 如果我们在打印 co.code 时将 System.out.println(Thread.currentThread().getName()); 上下移动,则行为相同。 有什么解释吗?
public class Concepts extends Thread {
int code = 9;
public void run() {
this.code = 7;
}
public static void main(String[] args) {
Concepts co = new Concepts();
co.start();
for (int i = 0; i < 5; i++) {
System.out.print(co.code + "==");
System.out.println(Thread.currentThread().getName());
}
}
}
【问题讨论】:
-
尝试正确使用代码格式
-
要了解原因,您必须了解一些关于
Threads 和并发的基础知识。看来你是expectingThread.start()andRunnable.run()to have identical semantics。 -
你需要一个线程和并发的教程。
标签: java multithreading core