Skip to content

Commit fbc3207

Browse files
committed
🐛 Fixing a bug.fix bug
1 parent e4a9762 commit fbc3207

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

MD/concurrent/volatile.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class Volatile implements Runnable{
3737
@Override
3838
public void run() {
3939
while (flag){
40-
System.out.println(Thread.currentThread().getName() + "正在运行。。。");
4140
}
4241
System.out.println(Thread.currentThread().getName() +"执行完毕");
4342
}
@@ -49,15 +48,30 @@ public class Volatile implements Runnable{
4948

5049
System.out.println("main 线程正在运行") ;
5150

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")){
5355

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("主线程退出了!");
5568

5669
}
5770

5871
private void stopThread(){
5972
flag = false ;
6073
}
74+
6175
}
6276
```
6377

src/main/java/com/crossoverjie/concurrent/Volatile.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.crossoverjie.concurrent;
22

3-
import java.util.concurrent.TimeUnit;
3+
import java.util.Scanner;
44

55
/**
66
* Function:
@@ -16,7 +16,6 @@ public class Volatile implements Runnable{
1616
@Override
1717
public void run() {
1818
while (flag){
19-
System.out.println(Thread.currentThread().getName() + "正在运行。。。");
2019
}
2120
System.out.println(Thread.currentThread().getName() +"执行完毕");
2221
}
@@ -28,13 +27,28 @@ public static void main(String[] args) throws InterruptedException {
2827

2928
System.out.println("main 线程正在运行") ;
3029

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")){
3234

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("主线程退出了!");
3447

3548
}
3649

3750
private void stopThread(){
3851
flag = false ;
3952
}
53+
4054
}

0 commit comments

Comments
 (0)