forked from vipstone/java-core-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
100 lines (81 loc) · 3.5 KB
/
Copy pathTest.java
File metadata and controls
100 lines (81 loc) · 3.5 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package example;
import java.util.Date;
import java.util.concurrent.*;
public class Test {
public static void main(String[] args) throws Exception {
// // ----------- BLOCKED 阻塞状态 --------------
// MyCounter myCounter = new MyCounter();
// // 线程1调用同步线程,模拟阻塞
// new Thread(() -> myCounter.increase()).start();
// // 线程2继续调用同步阻塞方法
// Thread example = new Thread(() -> myCounter.increase());
// example.start();
// // 让主线程等10毫秒
// Thread.currentThread().sleep(10);
// // 打印线程2,为阻塞状态:BLOCKED
// System.out.println(example.getState());
// // ----------- TERMINATED 状态 --------------
// Thread example = new Thread() {
// @Override
// public void run() {
// System.out.println(Thread.currentThread().getName());
// }
// };
// example.start();
// // 主线程挂起200毫秒,等thread执行完成
// Thread.sleep(200);
// // TERMINATED
// System.out.println(example.getState());
// // ----------- WAITING 或 TIMED_WAITING 状态 --------------
// Thread example = new Thread(new MyThread());
// example.start();
// // 主线程挂起200毫秒,等thread执行完成
// Thread.sleep(200);
// // 输出WAITING,线程thread一直处于被挂起状态
// System.out.println(example.getState());
// synchronized (MyThread.class) {
// MyThread.class.notify();
// }
//// ----------- TERMINATED 状态 --------------
// Thread example = new Thread(() -> System.out.println(Thread.currentThread().getName()));
// example.start();
//// 让主线程等10毫秒
// Thread.currentThread().sleep(10);
// System.out.println(example.getState());
// Thread example = new Thread(() -> System.out.println(Thread.currentThread().getName()));
// example.start();
// //----------- Callable Future 创建线程 --------------
// Callable<String> callable = new MyThread();
// FutureTask<String> ft = new FutureTask<>(callable);
// new Thread(ft, "threadName").start();
// System.out.println(ft.get());
// // ----------- sleep、wait持锁测试 --------------
// SynchronizedTest synchronizedTest = new SynchronizedTest();
// synchronizedTest.start();
// synchronizedTest.secord();
// //主线程稍等10毫秒
// Thread.sleep(10);
// System.out.println(synchronizedTest.number);
//
// System.out.println(Runtime.getRuntime().availableProcessors());
// ExecutorService es = Executors.newSingleThreadExecutor();
// ExecutorService es = Executors.newCachedThreadPool();
// ExecutorService es = Executors.newFixedThreadPool(6);
// ExecutorService es = Executors.newWorkStealingPool();
ExecutorService es = Executors.newScheduledThreadPool(5);
es.submit(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName()+ " | 时间:" + new Date().getTime());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// es.shutdown();
}
}