Skip to content

Commit 7a25714

Browse files
committed
Java并发
1 parent fdb2e94 commit 7a25714

8 files changed

Lines changed: 472 additions & 22 deletions

File tree

code/Java/java-concurrency/src/main/java/com/heibaiying/countDown/J2_CountDown.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class J2_CountDown {
99

1010
private static int number = 100;
11+
// 指定计数器的初始值
1112
private static CountDownLatch latch = new CountDownLatch(number);
1213
private static AtomicInteger integer = new AtomicInteger(0);
1314

@@ -19,7 +20,7 @@ public void run() {
1920
// 假设这是一个耗时的任务
2021
Thread.sleep(3000);
2122
integer.incrementAndGet();
22-
// 计数减一
23+
// 计数器减1
2324
latch.countDown();
2425
} catch (InterruptedException e) {
2526
e.printStackTrace();
@@ -33,8 +34,8 @@ public static void main(String[] args) throws InterruptedException {
3334
for (int i = 0; i < number; i++) {
3435
executorService.submit(task);
3536
}
37+
// 等待计数器为0时唤醒所有等待的线程
3638
latch.await();
37-
// 会等待所有任务执行完成再输出
3839
System.out.println("integer:" + integer);
3940
executorService.shutdown();
4041
}

code/Java/java-concurrency/src/main/java/com/heibaiying/semaphore/J1_Semaphore.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class J1_Semaphore {
66

7+
// 限制并发访问的线程的数量为5
78
private static Semaphore semaphore = new Semaphore(5);
89

910
static class IncreaseTask implements Runnable {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.heibaiying.stop;
2+
3+
/**
4+
* 线程终止
5+
*/
6+
public class ThreadStop {
7+
8+
private static volatile boolean stopFlag = true;
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
Thread thread = new Thread(() -> {
12+
while (stopFlag) {
13+
try {
14+
Thread.sleep(100);
15+
System.out.println("持续输出");
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
});
21+
thread.start();
22+
Thread.sleep(3 * 1000);
23+
stopFlag = false;
24+
System.out.println("线程终止");
25+
}
26+
}

code/Java/java-concurrency/src/main/java/com/heibaiying/threadPool/J1_ThreadPool.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ public class J1_ThreadPool {
1111
static class Task implements Runnable {
1212
@Override
1313
public void run() {
14-
System.out.println(Thread.currentThread().getName() + "正在执行");
14+
try {
15+
Thread.sleep(100);
16+
System.out.println(Thread.currentThread().getName() + "正在执行");
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
1520
}
1621
}
1722

1823
public static void main(String[] args) {
1924
ExecutorService executorService = Executors.newFixedThreadPool(10);
2025
for (int i = 0; i < 100; i++) {
26+
// 提交任务到线程池
2127
executorService.submit(new Task());
2228
}
29+
// 关闭线程池,此时不再接受新任务,但仍会等待原有的任务执行完成,如果想要立即关闭,则可以使用shutdownNow()
2330
executorService.shutdown();
2431
}
2532
}

code/Java/java-concurrency/src/main/java/com/heibaiying/threadPool/J2_ScheduledTask.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ public void run() {
3333
}
3434

3535
public static void main(String[] args) {
36+
// 为避免相互间的影响,以下各种场景最好分别测试:
3637
ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
3738
// 只执行一次
3839
pool.schedule(new Task("schedule"), 2, TimeUnit.SECONDS);
39-
// 指定2秒为固定周期执行,如果项目执行耗时5秒,则项目结束后立马执行下一次任务,所以输出的时间间隔为5秒
40+
// 指定2秒为固定周期执行,因为项目执行耗时5秒,此时项目结束会立马执行下一次任务,所以输出的时间间隔为5秒
4041
pool.scheduleAtFixedRate(new Task("FixedRate"), 0, 2, TimeUnit.SECONDS);
41-
// 总是在上一次项目结束后间隔指定周期执行,所以项目耗时5秒,还需要间隔2秒执行,所以输出的时间间隔为7秒
42+
// 总是在上一次项目结束后间隔指定周期执行,因为项目耗时5秒,还需要间隔2秒执行,所以输出的时间间隔为7秒
4243
pool.scheduleWithFixedDelay(new Task("WithFixedDelay"), 0, 2, TimeUnit.SECONDS);
4344
// pool.shutdown();
4445
}

code/Java/java-concurrency/src/main/java/com/heibaiying/yieldAndJoin/J1_Normal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class J1_Normal {
77

88
private static int j = 0;
99

10-
public static void main(String[] args) throws InterruptedException {
10+
public static void main(String[] args) {
1111
Thread thread = new Thread(() -> {
1212
for (int i = 0; i < 100000; i++) {
1313
j++;

0 commit comments

Comments
 (0)