Skip to content

Commit a7fee33

Browse files
Important Method
1 parent 2d79fbc commit a7fee33

15 files changed

+590
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Java并发编程
66
创建线程的两种方法
77
正确启动线程
88
正确停止线程
9+
线程六大生命周期
10+
Thread和Object中的重要方法
911
ThreadPool
1012
ThreadLocal
1113

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package importantmethod;
2+
3+
/**
4+
* @Author: zzStar
5+
* @Date: 10-17-2020 14:56
6+
*/
7+
public class MethodSummary {
8+
9+
/**
10+
* wait,notify,notifyAll
11+
* 1.用必须先拥有monitor
12+
* 2.只能唤醒其中一个,取决于jvm的实现
13+
* 3.都属于Object类,任何对象都可以调用,native final
14+
* 4.类似功能的Condition
15+
* 5.同时持有多个锁的情况,释放现在wait所对应的对象的那把锁,注意避免死锁的发生
16+
*/
17+
18+
/**
19+
* 特殊情况注意 ❗
20+
* -> 从Object.wait()状态刚被唤醒时,通常不能立刻抢到monitor锁,(因为其他线程唤醒也需要持有同一把锁)
21+
* 那就会从waiting先进入blocked状态,抢到锁后再转换到Runnable状态
22+
* -> 如果发生异常,可以直接跳转到终止Terminated状态,不必再遵循路径,比如可以从waiting直接到Terminated
23+
*/
24+
25+
/**
26+
* wait()需要放在同步代码块里面执行的原因是为了让通信变得可靠,防止死锁、永久等待的发生
27+
* 线程间需要协同配合的方式放入同步代码块中实现
28+
*/
29+
30+
/**
31+
* 为什么wait,notify,notifyAll被定义在Object里面?
32+
* ->锁级别的操作,属于对象,而非线程中
33+
* ->Thread.wait,线程退出的时候,源码中会自动notify
34+
*/
35+
36+
/**
37+
* Sleep ❗ 方法可以让线程进入waiting状态,并且不占用CPU资源,但是不释放锁,
38+
* 直到规定时间后再执行,休眠期间如果被中断,会抛出异常并清除中断状态
39+
*/
40+
41+
/**
42+
* wait ,notify ,sleep 异同
43+
* -> 相同 1.阻塞
44+
* 2.响应中断
45+
*
46+
* -> 不同 1.wait ,notify必须在同步方法(见上述原因)
47+
* 2.Sleep不释放锁
48+
* 3.sleep要指定时间,wait不传则直到自己被唤醒
49+
* 4.所属类不同(见上述原因)
50+
*/
51+
52+
/**
53+
* join 期间,线程处于waiting的状态
54+
*/
55+
56+
/**
57+
* yield释放CPU时间片,但不释放锁,也不会陷入阻塞,仍然Runnable
58+
*/
59+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package importantmethod.join;
2+
3+
/**
4+
* join期间被中断
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-18-2020 12:30
8+
*/
9+
public class JoinInterrupt {
10+
public static void main(String[] args) {
11+
Thread mainThread = Thread.currentThread();
12+
Thread thread = new Thread(() -> {
13+
try {
14+
// 中断主线程
15+
mainThread.interrupt();
16+
Thread.sleep(2000);
17+
System.out.println("Thread1 finished");
18+
} catch (InterruptedException e) {
19+
// e.printStackTrace();
20+
System.out.println("子线程中断");
21+
}
22+
});
23+
24+
thread.start();
25+
System.out.println("等待子线程运行完毕");
26+
try {
27+
// 但主线程在等待子线程
28+
thread.join();
29+
} catch (InterruptedException e) {
30+
System.out.println(Thread.currentThread().getName() + "主线程被中断了");
31+
// e.printStackTrace();
32+
thread.interrupt();
33+
}
34+
// 注意,子线程根本就没有运行完毕,仍继续打出Thread1 finished
35+
System.out.println("子线程执行完毕");
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package importantmethod.join;
2+
3+
/**
4+
* 通过join原理,分析出join的代替写法
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-18-2020 13:03
8+
*/
9+
public class JoinPrinciple {
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
Thread thread1 = new Thread(() -> {
13+
try {
14+
Thread.sleep(1000);
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
System.out.println(Thread.currentThread().getName() + "执行完毕");
19+
});
20+
21+
thread1.start();
22+
System.out.println("开始等待子线程运行完毕");
23+
// thread1.join();
24+
25+
// 等价代码
26+
synchronized (thread1) {
27+
// 直到thread1执行完毕才继续
28+
thread1.wait();
29+
}
30+
System.out.println("所有子线程全部执行完毕");
31+
}
32+
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package importantmethod.join;
2+
3+
/**
4+
* join线程状态
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-18-2020 12:48
8+
*/
9+
public class JoinThreadState {
10+
public static void main(String[] args) throws InterruptedException {
11+
Thread mainThread = Thread.currentThread();
12+
13+
Thread thread = new Thread(() -> {
14+
try {
15+
Thread.sleep(3000);
16+
System.out.println(mainThread.getState());//WAITING
17+
System.out.println("Thread-0运行结束🔚");
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
});
22+
thread.start();
23+
System.out.println("等待子线程运行完毕");
24+
thread.join();// 主线程等待
25+
System.out.println("子线程运行完毕");
26+
}
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package importantmethod.join;
2+
3+
/**
4+
* 新的线程加入了,要等待它执行完再出发
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-18-2020 12:11
8+
*/
9+
public class JoinUsage {
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
Thread thread1 = new Thread(() -> {
13+
try {
14+
Thread.sleep(1000);
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
System.out.println(Thread.currentThread().getName() + "执行完毕");
19+
});
20+
21+
Thread thread2 = new Thread(() -> {
22+
try {
23+
Thread.sleep(1000);
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
System.out.println(Thread.currentThread().getName() + "执行完毕");
28+
});
29+
30+
thread1.start();
31+
thread2.start();
32+
System.out.println("开始等待子线程运行完毕");
33+
//使得主线程main等待其执行完毕
34+
thread1.join();
35+
thread2.join();
36+
System.out.println("所有子线程全部执行完毕");
37+
}
38+
}
345 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package importantmethod.sleep;
2+
3+
import java.util.Date;
4+
import java.util.concurrent.TimeUnit;
5+
6+
/**
7+
* 实现每一秒钟输出当前时间,被中断,观察
8+
*
9+
* @Author: zzStar
10+
* @Date: 10-18-2020 11:18
11+
*/
12+
public class SleepInterrupted implements Runnable {
13+
14+
public static void main(String[] args) throws InterruptedException {
15+
SleepInterrupted sleepInterrupted = new SleepInterrupted();
16+
Thread thread = new Thread(sleepInterrupted);
17+
thread.start();
18+
Thread.sleep(5000);
19+
thread.interrupt();
20+
}
21+
22+
@Override
23+
public void run() {
24+
for (int i = 0; i < 10; i++) {
25+
System.out.println(new Date());
26+
try {
27+
TimeUnit.SECONDS.sleep(1);
28+
} catch (InterruptedException e) {
29+
System.out.println("被中断");
30+
e.printStackTrace();
31+
}
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package importantmethod.sleep;
2+
3+
/**
4+
* 1.让线程在预期的时间执行,其他时候不要占用cpu资源
5+
* 2.不释放锁
6+
*
7+
* @Author: zzStar
8+
* @Date: 10-17-2020 16:31
9+
*/
10+
public class SleepUsage implements Runnable {
11+
12+
public static void main(String[] args) {
13+
SleepUsage sleepUsage = new SleepUsage();
14+
Thread thread1 = new Thread(sleepUsage);
15+
Thread thread2 = new Thread(sleepUsage);
16+
thread1.start();
17+
thread2.start();
18+
}
19+
20+
@Override
21+
public void run() {
22+
syn();
23+
}
24+
25+
private synchronized void syn() {
26+
System.out.println(Thread.currentThread().getName() + "获取到了monitor");
27+
try {
28+
Thread.sleep(2000);
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
}
32+
System.out.println(Thread.currentThread().getName() + "退出了同步代码块");
33+
}
34+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package importantmethod.waitnotify;
2+
3+
import java.util.Date;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* 用wait/notify实现生产消费交替进行
8+
*
9+
* @Author: zzStar
10+
* @Date: 10-17-2020 15:28
11+
*/
12+
public class ProducerConsumerModel {
13+
14+
public static void main(String[] args) {
15+
EventStorage eventStorage = new EventStorage();
16+
Producer producer = new Producer(eventStorage);
17+
Consumer consumer = new Consumer(eventStorage);
18+
new Thread(producer).start();
19+
new Thread(consumer).start();
20+
}
21+
}
22+
23+
class Producer implements Runnable {
24+
private EventStorage storage;
25+
26+
public Producer(EventStorage storage) {
27+
this.storage = storage;
28+
}
29+
30+
@Override
31+
public void run() {
32+
for (int i = 0; i < 100; i++) {
33+
storage.put();
34+
}
35+
}
36+
}
37+
38+
class Consumer implements Runnable {
39+
private EventStorage storage;
40+
41+
public Consumer(EventStorage storage) {
42+
this.storage = storage;
43+
}
44+
45+
46+
@Override
47+
public void run() {
48+
for (int i = 0; i < 100; i++) {
49+
storage.take();
50+
}
51+
}
52+
}
53+
54+
class EventStorage {
55+
private int maxSize;
56+
private LinkedList<Date> storage;
57+
58+
public EventStorage() {
59+
maxSize = 10;
60+
storage = new LinkedList<>();
61+
}
62+
63+
public synchronized void put() {
64+
while (storage.size() == maxSize) {
65+
try {
66+
wait();
67+
} catch (InterruptedException e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
storage.add(new Date());
72+
System.out.println("仓库里有了" + storage.size() + "个产品");
73+
//唤醒消费者
74+
notify();
75+
}
76+
77+
public synchronized void take() {
78+
while (storage.size() == 0) {
79+
try {
80+
wait();
81+
} catch (InterruptedException e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
// System.out.println("拿到了" + storage.get(0) + ",现在仓库还剩下" + storage.size());
86+
// storage.remove(0);
87+
System.out.println("拿到了" + storage.poll() + ",现在仓库还剩下" + storage.size());
88+
notify();
89+
}
90+
}
91+

0 commit comments

Comments
 (0)