-
Notifications
You must be signed in to change notification settings - Fork 320
/
NoPublishDemo.java
43 lines (36 loc) · 1.42 KB
/
NoPublishDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package fucking.concurrency.demo;
/**
* <a href="https://hllvm-group.iteye.com/group/topic/34932">请问R大 有没有什么工具可以查看正在运行的类的c/汇编代码</a>提到了<b>代码提升</b>的问题。
*
* @author Jerry Lee (oldratlee at gmail dot com)
*/
public class NoPublishDemo {
boolean stop = false;
public static void main(String[] args) throws Exception {
// LoadMaker.makeLoad();
NoPublishDemo demo = new NoPublishDemo();
Thread thread = new Thread(demo.getConcurrencyCheckTask());
thread.start();
Thread.sleep(1000);
System.out.println("Set stop to true in main!");
demo.stop = true;
System.out.println("Exit main.");
}
ConcurrencyCheckTask getConcurrencyCheckTask() {
return new ConcurrencyCheckTask();
}
private class ConcurrencyCheckTask implements Runnable {
@Override
@SuppressWarnings({"WhileLoopSpinsOnField", "StatementWithEmptyBody"})
public void run() {
System.out.println("ConcurrencyCheckTask started!");
// If the value of stop is visible in the main thread, the loop will exit.
// On my dev machine, the loop almost never exits!
// Simple and safe solution:
// add volatile to the `stop` field.
while (!stop) {
}
System.out.println("ConcurrencyCheckTask stopped!");
}
}
}