Skip to content

Commit e660cc5

Browse files
committed
更新
1 parent 009a183 commit e660cc5

File tree

10 files changed

+317
-113
lines changed

10 files changed

+317
-113
lines changed

JavaConcurrent/.idea/compiler.xml

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

JavaConcurrent/.idea/encodings.xml

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

JavaConcurrent/.idea/jarRepositories.xml

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

JavaConcurrent/.idea/misc.xml

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

JavaConcurrent/.idea/vcs.xml

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

JavaConcurrent/.idea/workspace.xml

Lines changed: 152 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaConcurrent/JavaConcurrent.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3-
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
44
<output url="file://$MODULE_DIR$/target/classes" />
55
<output-test url="file://$MODULE_DIR$/target/test-classes" />
66
<content url="file://$MODULE_DIR$">
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.FlammulinaBlog.Java.Concurrent.CAS;
2+
3+
import java.util.List;
4+
import java.util.concurrent.ArrayBlockingQueue;
5+
import java.util.concurrent.CountDownLatch;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
public class AtomicCounter {
9+
10+
private AtomicInteger value = new AtomicInteger(0);
11+
12+
public int getValue() {
13+
return value.get();
14+
}
15+
16+
public void increment() {
17+
int current;
18+
int next;
19+
do {
20+
// 读取当前值
21+
current = value.get();
22+
// 计算下一个值
23+
next = current + 1;
24+
// CAS操作尝试更新值
25+
System.out.println(Thread.currentThread().getName() + ": CAS operation current: " + current + " next:" + next + (current == next ? "succeeded" : "failed"));
26+
} while (!value.compareAndSet(current, next)); // 如果当前值已经被其他线程修改,则重试
27+
// 打印CAS操作结果
28+
}
29+
30+
public static void main(String[] args) {
31+
AtomicCounter counter = new AtomicCounter();
32+
// 启动多个线程并发执行递增操作
33+
for (int i = 0; i < 100; i++) {
34+
new Thread(() -> {
35+
for (int j = 0; j < 1000; j++) {
36+
counter.increment();
37+
}
38+
}).start();
39+
}
40+
41+
ArrayBlockingQueue sss = new ArrayBlockingQueue<>(1);
42+
43+
CountDownLatch countDownLatch = new CountDownLatch(2);
44+
45+
// 等待所有线程执行完毕
46+
try {
47+
Thread.sleep(1000);
48+
} catch (InterruptedException e) {
49+
e.printStackTrace();
50+
}
51+
52+
// 输出最终计数值
53+
System.out.println("Final count: " + counter.getValue());
54+
}
55+
56+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.FlammulinaBlog.Java.Concurrent;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
public class CountDownLatchExample {
6+
private static final int THREAD_COUNT = 64;
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
CountDownLatch startSignal = new CountDownLatch(1);
10+
CountDownLatch doneSignal = new CountDownLatch(THREAD_COUNT);
11+
12+
for (int i = 0; i < THREAD_COUNT; i++) {
13+
new Thread(() -> {
14+
try {
15+
startSignal.await(); // 等待startSignal为0
16+
// 模拟任务执行
17+
System.out.println("Thread " + Thread.currentThread().getName() + " is executing task...");
18+
Thread.sleep(1000); // 模拟任务耗时
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
} finally {
22+
doneSignal.countDown(); // 任务执行完毕,计数器减1
23+
}
24+
}).start();
25+
}
26+
27+
System.out.println("Start all threads...");
28+
29+
Thread.sleep(1000L);
30+
startSignal.countDown(); // 触发所有线程开始执行任务
31+
System.out.println("触发所有线程开始执行任务");
32+
doneSignal.await(); // 等待所有线程执行完毕
33+
System.out.println("All threads have finished executing tasks.");
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.FlammulinaBlog.Java.Concurrent.Semaphore;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
public class SemaphoreExample {
6+
7+
public static void main(String[] args) {
8+
9+
Semaphore semaphore = new Semaphore(20); // 创建一个初始许可证数量为 3 的 Semaphore
10+
11+
// 创建 5 个线程并启动
12+
for (int i = 0; i < 10; i++) {
13+
Thread thread = new Thread(new Worker(semaphore, i + 1));
14+
thread.start();
15+
}
16+
17+
}
18+
19+
static class Worker implements Runnable {
20+
private final Semaphore semaphore;
21+
private final int id;
22+
23+
public Worker(Semaphore semaphore, int id) {
24+
this.semaphore = semaphore;
25+
this.id = id;
26+
}
27+
28+
@Override
29+
public void run() {
30+
try {
31+
semaphore.acquire(); // 获取许可证
32+
System.out.println("Worker " + id + " is 工作中");
33+
Thread.sleep(2000); // 模拟工作
34+
System.out.println("Worker " + id + " 完成工作");
35+
semaphore.release(); // 释放许可证
36+
} catch (InterruptedException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
}
41+
42+
}

0 commit comments

Comments
 (0)