Skip to content

Commit f72f232

Browse files
committed
更新线程状态,终止
1 parent 7f35a7b commit f72f232

19 files changed

+178
-14
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

concurrency/src/main/java/com/javaedge/concurrency/ConcurrencyApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
99

1010
/**
11-
* @author sss
11+
* @author JavaEdge
1212
*/
1313
@SpringBootApplication
1414
public class ConcurrencyApplication extends WebMvcConfigurerAdapter{

concurrency/src/main/java/com/javaedge/concurrency/ConcurrencyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.concurrent.Semaphore;
1010

1111
/**
12-
* @author sss
12+
* @author JavaEdge
1313
*/
1414
@Slf4j
1515
@NotThreadSafe
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.javaedge.concurrency.common.state;
2+
3+
/**
4+
* 多线程运行状态切换示例
5+
*
6+
* @author JavaEdge
7+
* @date 2019/8/26
8+
*/
9+
public class ThreadState {
10+
public static Thread thread1;
11+
public static ThreadState obj;
12+
13+
public static void main(String[] args) throws Exception {
14+
15+
// 第一种状态切换 - 新建 -> 运行 -> 终止
16+
System.out.println("======= 第一种状态切换 - 新建 -> 运行 -> 终止 =======");
17+
18+
Thread thread1 = new Thread(() -> {
19+
System.out.println("thread1当前状态:" + Thread.currentThread().getState().toString());
20+
System.out.println("thread1 执行");
21+
});
22+
23+
System.out.println("未调用start方法,thread1当前状态:" + thread1.getState().toString());
24+
25+
thread1.start();
26+
27+
// 等待thread1执行结束,再看状态
28+
Thread.sleep(2000L);
29+
System.out.println("等待2s,再看thread1当前状态:" + thread1.getState().toString());
30+
// thread1.start(); 线程终止之后,再调用start方法,会抛IllegalThreadStateException
31+
32+
System.out.println("-------------------------------------");
33+
System.out.println("======= 第二种:新建 -> 运行 -> 等待 -> 运行 -> 终止(sleep方式) =======");
34+
Thread thread2 = new Thread(() -> {
35+
try {
36+
// 将线程2移动到等待状态,1500ms后自动唤醒
37+
Thread.sleep(1500);
38+
} catch (InterruptedException e) {
39+
e.printStackTrace();
40+
}
41+
System.out.println("thread2当前状态:" + Thread.currentThread().getState().toString());
42+
System.out.println("thread2 执行");
43+
});
44+
System.out.println("未调用start方法,thread2当前状态:" + thread2.getState().toString());
45+
thread2.start();
46+
System.out.println("调用start方法,thread2当前状态:" + thread2.getState().toString());
47+
// 等待200毫秒,再看状态
48+
Thread.sleep(200L);
49+
System.out.println("等待200毫秒,再看thread2当前状态:" + thread2.getState().toString());
50+
// 再等待3秒,让thread2执行完毕,再看状态
51+
Thread.sleep(3000L);
52+
System.out.println("等待3秒,再看thread2当前状态:" + thread2.getState().toString());
53+
54+
System.out.println("-------------------------------------");
55+
System.out.println("======= 第三种:新建 -> 运行 -> 阻塞 -> 运行 -> 终止 =======");
56+
Thread thread3 = new Thread(() -> {
57+
synchronized (ThreadState.class) {
58+
System.out.println("thread3当前状态:" + Thread.currentThread().getState().toString());
59+
System.out.println("thread3 执行了");
60+
}
61+
});
62+
synchronized (ThreadState.class) {
63+
System.out.println("未调用start方法,thread3当前状态:" + thread3.getState().toString());
64+
thread3.start();
65+
System.out.println("调用start方法,thread3当前状态:" + thread3.getState().toString());
66+
// 等待200毫秒,再看状态
67+
Thread.sleep(200L);
68+
System.out.println("等待200毫秒,再看thread3当前状态:" + thread3.getState().toString());
69+
}
70+
// 再等待3秒,让thread3执行完毕,再看状态
71+
Thread.sleep(3000L);
72+
System.out.println("等待3秒,让thread3抢到锁,再看thread3当前状态:" + thread2.getState().toString());
73+
74+
}
75+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.javaedge.concurrency.common.stop;
2+
3+
/**
4+
* 通过状态位来判断
5+
*
6+
* @author JavaEdge
7+
* @date 2019/8/29
8+
*/
9+
public class FlagStopThread extends Thread {
10+
public volatile static boolean flag = true;
11+
12+
public static void main(String[] args) throws InterruptedException {
13+
new Thread(() -> {
14+
try {
15+
while (flag) { // 判断是否运行
16+
System.out.println("运行中");
17+
Thread.sleep(1000L);
18+
}
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
}).start();
23+
// 3秒之后,将状态标志改为False,代表不继续运行
24+
Thread.sleep(3000L);
25+
flag = false;
26+
System.out.println("程序运行结束");
27+
}
28+
}
29+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.javaedge.concurrency.common.stop;
2+
3+
/**
4+
* 线程stop强制性中止,破坏线程安全的示例
5+
*
6+
* @author JavaEdge
7+
* @date 2019/8/26
8+
*/
9+
public class StopThread extends Thread {
10+
private int i = 0, j = 0;
11+
12+
@Override
13+
public void run() {
14+
synchronized (this) {
15+
// 增加同步锁,确保线程安全
16+
++i;
17+
try {
18+
// 休眠10秒,模拟耗时操作
19+
Thread.sleep(10000);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
++j;
24+
}
25+
}
26+
27+
/**
28+
* 打印i和j
29+
*/
30+
public void print() {
31+
System.out.println("i=" + i + " j=" + j);
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.javaedge.concurrency.common.stop;
2+
3+
/**
4+
* 线程stop强制性中止,破坏线程安全的示例
5+
*
6+
* @author JavaEdge
7+
* @date 2019/8/26
8+
*/
9+
public class ThreadStop {
10+
public static void main(String[] args) throws InterruptedException {
11+
StopThread thread = new StopThread();
12+
thread.start();
13+
// 休眠1秒,确保i变量自增成功
14+
Thread.sleep(1000);
15+
// 暂停线程
16+
// thread.stop(); // 错误的终止
17+
thread.interrupt(); // 正确的终止
18+
while (thread.isAlive()) {
19+
// 确保线程已经终止
20+
} // 输出结果
21+
thread.print();
22+
}
23+
}
24+
25+
26+

concurrency/src/main/java/com/javaedge/concurrency/example/cache/CacheController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.springframework.web.bind.annotation.ResponseBody;
88

99
/**
10-
* @author sss
10+
* @author JavaEdge
1111
*/
1212
@Controller
1313
@RequestMapping("/cache")

concurrency/src/main/java/com/javaedge/concurrency/example/cache/RedisClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import javax.annotation.Resource;
88

99
/**
10-
* @author sss
10+
* @author JavaEdge
1111
*/
1212
@Component
1313
public class RedisClient {

concurrency/src/main/java/com/javaedge/concurrency/example/cache/RedisConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import redis.clients.jedis.JedisPool;
77

88
/**
9-
* @author sss
9+
* @author JavaEdge
1010
*/
1111
@Configuration
1212
public class RedisConfig {

0 commit comments

Comments
 (0)