File tree 2 files changed +35
-7
lines changed
src/main/java/com/crossoverjie/concurrent
2 files changed +35
-7
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,6 @@ public class Volatile implements Runnable{
37
37
@Override
38
38
public void run () {
39
39
while (flag){
40
- System . out. println(Thread . currentThread(). getName() + " 正在运行。。。" );
41
40
}
42
41
System . out. println(Thread . currentThread(). getName() + " 执行完毕" );
43
42
}
@@ -49,15 +48,30 @@ public class Volatile implements Runnable{
49
48
50
49
System . out. println(" main 线程正在运行" ) ;
51
50
52
- TimeUnit . MILLISECONDS. sleep(100 ) ;
51
+ Scanner sc = new Scanner (System . in);
52
+ while (sc. hasNext()){
53
+ String value = sc. next();
54
+ if (value. equals(" 1" )){
53
55
54
- aVolatile. stopThread();
56
+ new Thread (new Runnable () {
57
+ @Override
58
+ public void run () {
59
+ aVolatile. stopThread();
60
+ }
61
+ }). start();
62
+
63
+ break ;
64
+ }
65
+ }
66
+
67
+ System . out. println(" 主线程退出了!" );
55
68
56
69
}
57
70
58
71
private void stopThread (){
59
72
flag = false ;
60
73
}
74
+
61
75
}
62
76
```
63
77
Original file line number Diff line number Diff line change 1
1
package com .crossoverjie .concurrent ;
2
2
3
- import java .util .concurrent . TimeUnit ;
3
+ import java .util .Scanner ;
4
4
5
5
/**
6
6
* Function:
@@ -16,7 +16,6 @@ public class Volatile implements Runnable{
16
16
@ Override
17
17
public void run () {
18
18
while (flag ){
19
- System .out .println (Thread .currentThread ().getName () + "正在运行。。。" );
20
19
}
21
20
System .out .println (Thread .currentThread ().getName () +"执行完毕" );
22
21
}
@@ -28,13 +27,28 @@ public static void main(String[] args) throws InterruptedException {
28
27
29
28
System .out .println ("main 线程正在运行" ) ;
30
29
31
- TimeUnit .MILLISECONDS .sleep (100 ) ;
30
+ Scanner sc = new Scanner (System .in );
31
+ while (sc .hasNext ()){
32
+ String value = sc .next ();
33
+ if (value .equals ("1" )){
32
34
33
- aVolatile .stopThread ();
35
+ new Thread (new Runnable () {
36
+ @ Override
37
+ public void run () {
38
+ aVolatile .stopThread ();
39
+ }
40
+ }).start ();
41
+
42
+ break ;
43
+ }
44
+ }
45
+
46
+ System .out .println ("主线程退出了!" );
34
47
35
48
}
36
49
37
50
private void stopThread (){
38
51
flag = false ;
39
52
}
53
+
40
54
}
You can’t perform that action at this time.
0 commit comments