-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadVolatile1.java
More file actions
45 lines (36 loc) · 999 Bytes
/
Copy pathThreadVolatile1.java
File metadata and controls
45 lines (36 loc) · 999 Bytes
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
44
45
package secondPart;
/**
* @author yangxp
* @date 2017年8月7日 下午3:46:40
* <p>
* volatile 关键字;主要是理解公有栈堆和线程的私有栈堆
*/
public class ThreadVolatile1 {
public static void main(String[] args) {
ThreadVolatile1Test threadVolatile1Test = new ThreadVolatile1Test();
threadVolatile1Test.start();
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
threadVolatile1Test.setIsRunning(false);
System.out.println("end ======== " + Thread.currentThread().getName());
}
}
class ThreadVolatile1Test extends Thread {
private volatile boolean isRunning = true;
public boolean isRunning() {
return isRunning;
}
public void setIsRunning(boolean isRunning) {
this.isRunning = isRunning;
}
@Override
public void run() {
System.out.println("进入了 run");
while (isRunning == true) {
}
System.out.println("线程被停止了");
}
}