-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
319 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...t/src/main/java/io/github/dunwu/javacore/concurrent/container/ArrayBlockingQueueDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.github.dunwu.javacore.concurrent.container; | ||
|
||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.BlockingQueue; | ||
|
||
/** | ||
* BlockingQueue 示例 | ||
* | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @date 2024-07-15 | ||
*/ | ||
public class ArrayBlockingQueueDemo { | ||
|
||
public static final String EXIT_MSG = "Good bye!"; | ||
|
||
public static void main(String[] args) { | ||
// 使用较小的队列,以更好地在输出中展示其影响 | ||
BlockingQueue<String> queue = new ArrayBlockingQueue<>(3); | ||
Producer producer = new Producer(queue); | ||
Consumer consumer = new Consumer(queue); | ||
new Thread(producer).start(); | ||
new Thread(consumer).start(); | ||
} | ||
|
||
static class Producer implements Runnable { | ||
|
||
private BlockingQueue<String> queue; | ||
|
||
public Producer(BlockingQueue<String> q) { | ||
this.queue = q; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < 20; i++) { | ||
try { | ||
Thread.sleep(5L); | ||
String msg = "Message" + i; | ||
System.out.println("Produced new item: " + msg); | ||
queue.put(msg); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
try { | ||
System.out.println("Time to say good bye!"); | ||
queue.put(EXIT_MSG); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} | ||
|
||
static class Consumer implements Runnable { | ||
|
||
private BlockingQueue<String> queue; | ||
|
||
public Consumer(BlockingQueue<String> q) { | ||
this.queue = q; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String msg; | ||
while (!EXIT_MSG.equalsIgnoreCase((msg = queue.take()))) { | ||
System.out.println("Consumed item: " + msg); | ||
Thread.sleep(10L); | ||
} | ||
System.out.println("Got exit message, bye!"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
// 输出示例: | ||
// | ||
// Produced new item: Message0 | ||
// Consumed item: Message0 | ||
// Produced new item: Message1 | ||
// Consumed item: Message1 | ||
// Produced new item: Message2 | ||
// Produced new item: Message3 | ||
// Consumed item: Message2 | ||
// Produced new item: Message4 | ||
// Produced new item: Message5 | ||
// Consumed item: Message3 | ||
// Produced new item: Message6 | ||
// Produced new item: Message7 | ||
// Consumed item: Message4 | ||
// Produced new item: Message8 | ||
// Consumed item: Message5 | ||
// Produced new item: Message9 | ||
// Consumed item: Message6 | ||
// Produced new item: Message10 | ||
// Consumed item: Message7 | ||
// Produced new item: Message11 | ||
// Consumed item: Message8 | ||
// Produced new item: Message12 | ||
// Consumed item: Message9 | ||
// Produced new item: Message13 | ||
// Consumed item: Message10 | ||
// Produced new item: Message14 | ||
// Consumed item: Message11 | ||
// Produced new item: Message15 | ||
// Consumed item: Message12 | ||
// Produced new item: Message16 | ||
// Consumed item: Message13 | ||
// Produced new item: Message17 | ||
// Consumed item: Message14 | ||
// Produced new item: Message18 | ||
// Consumed item: Message15 | ||
// Produced new item: Message19 | ||
// Consumed item: Message16 | ||
// Time to say good bye! | ||
// Consumed item: Message17 | ||
// Consumed item: Message18 | ||
// Consumed item: Message1 |
47 changes: 47 additions & 0 deletions
47
...t/src/main/java/io/github/dunwu/javacore/concurrent/forkjoin/CompletableFutureDemo02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.github.dunwu.javacore.concurrent.forkjoin; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @date 2024-02-21 | ||
*/ | ||
public class CompletableFutureDemo02 { | ||
|
||
public static void main(String[] args) { | ||
noException(); | ||
catchException(); | ||
catchException2(); | ||
} | ||
|
||
public static void noException() { | ||
try { | ||
CompletableFuture.runAsync(() -> { | ||
int num = 1 / 0; | ||
}); | ||
} catch (Exception e) { | ||
System.err.println("noException: " + e.getMessage()); | ||
} | ||
} | ||
|
||
public static void catchException() { | ||
try { | ||
CompletableFuture.runAsync(() -> { | ||
int num = 1 / 0; | ||
}).get(); | ||
} catch (Exception e) { | ||
System.err.println("catchException: " + e.getMessage()); | ||
} | ||
} | ||
|
||
public static void catchException2() { | ||
try { | ||
CompletableFuture.runAsync(() -> { | ||
int num = 1 / 0; | ||
}).join(); | ||
} catch (Exception e) { | ||
System.err.println("catchException2: " + e.getMessage()); | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...acore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/sync/VolatileDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.dunwu.javacore.concurrent.sync; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class VolatileDemo { | ||
|
||
public volatile static int count = 0; | ||
|
||
public void add() { | ||
count++; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
ExecutorService threadPool = Executors.newFixedThreadPool(5); | ||
VolatileDemo volatileAtomicityDemo = new VolatileDemo(); | ||
for (int i = 0; i < 5; i++) { | ||
threadPool.execute(() -> { | ||
for (int j = 0; j < 500; j++) { | ||
volatileAtomicityDemo.add(); | ||
} | ||
}); | ||
} | ||
// 等待 1.5 秒,保证上面程序执行完成 | ||
Thread.sleep(1500); | ||
System.out.println(count); | ||
threadPool.shutdown(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.