FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

JDK juc learning · ablejava/ablejava-multithread@601ba6a · GitHub

Commit 601ba6a

Browse files
xiazhongwei
committed
JDK juc learning
1 parent a2045e8 commit 601ba6a

4 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.zhihuishu.thread.countDownLatch;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.CountDownLatch;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
8+
/**
9+
* 模拟火箭点火,所有自检完毕后才进行点火
10+
* Created by Administrator on 2019/2/24.
11+
*/
12+
public class CountDownLatchDemo implements Runnable {
13+
static final CountDownLatch end = new CountDownLatch(10);
14+
static final CountDownLatchDemo demo = new CountDownLatchDemo();
15+
16+
@Override
17+
public void run() {
18+
19+
// 模拟检查任务
20+
try {
21+
Thread.sleep(new Random().nextInt(10) *10000);
22+
System.out.print("check complete");
23+
end.countDown();
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
public static void main(String[] args) throws InterruptedException {
30+
ExecutorService exec = Executors.newFixedThreadPool(10);
31+
for (int i = 0; i < 10; i++) {
32+
exec.submit(demo);
33+
}
34+
// 等待检查
35+
end.await();
36+
System.out.print("fire!");
37+
exec.shutdown();
38+
}
39+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.zhihuishu.thread.cyclicBarrier;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.BrokenBarrierException;
5+
import java.util.concurrent.CyclicBarrier;
6+
7+
/**
8+
* Created by Administrator on 2019/2/24.
9+
*/
10+
public class CyclicBarrierDemo {
11+
12+
public static class Soldier implements Runnable{
13+
14+
private String soldier;
15+
private final CyclicBarrier cyclic;
16+
Soldier(CyclicBarrier cyclic, String soldierName) {
17+
this.cyclic = cyclic;
18+
this.soldier = soldierName;
19+
}
20+
@Override
21+
public void run() {
22+
23+
try {
24+
// 等待所有士兵到齐
25+
cyclic.await();
26+
// 等待所有士兵完成工作
27+
doWork();
28+
cyclic.await();
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
} catch (BrokenBarrierException e) {
32+
e.printStackTrace();
33+
}
34+
35+
}
36+
37+
void doWork(){
38+
try {
39+
Thread.sleep(Math.abs(new Random().nextInt() % 10000));
40+
41+
} catch (InterruptedException e) {
42+
e.printStackTrace();
43+
}
44+
System.out.println(soldier+":任务完成!");
45+
}
46+
}
47+
48+
public static class BarrierRun implements Runnable {
49+
boolean flag;
50+
int N;
51+
52+
public BarrierRun(boolean flag, int N) {
53+
this.flag = false;
54+
this.N = N;
55+
}
56+
@Override
57+
public void run() {
58+
if (flag) {
59+
System.out.println("司令:士兵"+N+"任务完成");
60+
} else {
61+
System.out.println("司令:士兵"+N+"集合完成");
62+
flag = true;
63+
}
64+
}
65+
}
66+
67+
68+
public static void main(String[] args) {
69+
final int N = 10;
70+
Thread[] allSoldier = new Thread[N];
71+
boolean flag = false;
72+
CyclicBarrier cyclic = new CyclicBarrier(N, new BarrierRun(flag, N));
73+
// 设置屏障点 ,主要是为了执行这个方法
74+
System.out.println("集合队伍!");
75+
for (int i = 0; i < N; i++) {
76+
System.out.println("士兵"+i+"报道");
77+
allSoldier[i] = new Thread(new Soldier(cyclic, "士兵"+i));
78+
allSoldier[i].start();
79+
}
80+
}
81+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.zhihuishu.thread.lockSupport;
2+
3+
import com.zhihuishu.thread.suspendAndResume.GoodSuspend;
4+
5+
import java.util.concurrent.locks.LockSupport;
6+
7+
/**
8+
* Created by Administrator on 2019/2/24.
9+
* 线程阻塞工具
10+
* 使用类似信号量的机制,每个线程只有一个许可,如果许可可用那么park()函数就会立即返回,并且消费这个许可,
11+
* 如果许可不可用,就会阻塞,而unpark()则使得一个许可变为可用,
12+
*/
13+
public class LockSupportDemo {
14+
public static Object u = new Object();
15+
static ChangeObjectThread t1 = new ChangeObjectThread("t1");
16+
static ChangeObjectThread t2 = new ChangeObjectThread("t2");
17+
18+
public static class ChangeObjectThread extends Thread{
19+
public ChangeObjectThread(String name) {
20+
super.setName(name);
21+
}
22+
23+
@Override
24+
public void run() {
25+
synchronized (u) {
26+
System.out.println("in "+ getName());
27+
LockSupport.park();
28+
}
29+
}
30+
}
31+
32+
33+
public static void main(String[] args) throws InterruptedException {
34+
t1.start();
35+
Thread.sleep(100);
36+
t2.start();
37+
LockSupport.unpark(t1);
38+
LockSupport.unpark(t2);
39+
t1.join();
40+
t2.join();
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.zhihuishu.thread.semaphore;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Semaphore;
6+
7+
/**
8+
* Created by Administrator on 2019/2/24.
9+
*/
10+
public class SemapDemo implements Runnable {
11+
final Semaphore semp = new Semaphore(5);
12+
@Override
13+
public void run() {
14+
15+
try {
16+
semp.acquire();
17+
// 模拟耗时操作
18+
Thread.sleep(2000);
19+
System.out.println(Thread.currentThread().getId()+": done !");
20+
semp.release();
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
25+
}
26+
27+
public static void main(String[] args) {
28+
ExecutorService exec = Executors.newFixedThreadPool(20);
29+
final SemapDemo demo = new SemapDemo();
30+
for (int i = 0; i < 20; i++) {
31+
exec.submit(demo);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL