From de6e74ca8ba9e0f3e746051064a3129d70711fb5 Mon Sep 17 00:00:00 2001 From: dunwu Date: Wed, 17 Jul 2024 08:29:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=9F=E4=B8=80=20pom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/javacore-advanced/pom.xml | 30 ++--- codes/javacore-basics/pom.xml | 21 +-- .../dunwu/javacore/variable/VariableDemo.java | 21 ++- codes/javacore-concurrent/pom.xml | 21 +-- .../container/ArrayBlockingQueueDemo.java | 123 ++++++++++++++++++ .../forkjoin/CompletableFutureDemo02.java | 47 +++++++ .../concurrent/sync/VolatileDemo.java | 30 +++++ codes/javacore-container/pom.xml | 21 +-- .../container/list/ArraysAsListDemo.java | 2 +- .../container/list/ListSubListDemo.java | 14 +- codes/javacore-effective/pom.xml | 21 +-- codes/javacore-in-web/pom.xml | 2 +- codes/javacore-io/pom.xml | 21 +-- codes/javacore-jdk8/pom.xml | 22 ++-- codes/javacore-utils/pom.xml | 17 ++- pom.xml | 28 ++++ 16 files changed, 319 insertions(+), 122 deletions(-) create mode 100644 codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/container/ArrayBlockingQueueDemo.java create mode 100644 codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/forkjoin/CompletableFutureDemo02.java create mode 100644 codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/sync/VolatileDemo.java rename "codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/AsList\347\244\272\344\276\213.java" => codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ArraysAsListDemo.java (98%) rename "codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/SubList\347\244\272\344\276\213.java" => codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ListSubListDemo.java (85%) diff --git a/codes/javacore-advanced/pom.xml b/codes/javacore-advanced/pom.xml index 7d30a4c1..661f8e8a 100644 --- a/codes/javacore-advanced/pom.xml +++ b/codes/javacore-advanced/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-advanced - 1.0.1 JavaCore :: Advanced @@ -45,26 +50,19 @@ - org.junit.platform - junit-platform-launcher + org.junit.jupiter + junit-jupiter test + + + + + org.assertj assertj-core test - - - - - io.github.dunwu - dunwu-dependencies - 1.1.2 - pom - import - - - diff --git a/codes/javacore-basics/pom.xml b/codes/javacore-basics/pom.xml index 9d816aad..bbefc92e 100644 --- a/codes/javacore-basics/pom.xml +++ b/codes/javacore-basics/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-basics - 1.0.1 JavaCore :: Basics @@ -34,16 +39,4 @@ logback-classic - - - - - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import - - - diff --git a/codes/javacore-basics/src/main/java/io/github/dunwu/javacore/variable/VariableDemo.java b/codes/javacore-basics/src/main/java/io/github/dunwu/javacore/variable/VariableDemo.java index c0d27f59..f9a53588 100644 --- a/codes/javacore-basics/src/main/java/io/github/dunwu/javacore/variable/VariableDemo.java +++ b/codes/javacore-basics/src/main/java/io/github/dunwu/javacore/variable/VariableDemo.java @@ -2,15 +2,24 @@ public class VariableDemo { - // 类变量(静态变量) - private static int v1 = 0; + // 静态变量 + private static String v1 = "静态变量"; - // 实例变量 - private String v2 = "word"; + // 成员变量 + private String v2 = "成员变量"; - public void method() { + public void test(String v4) { // 局部变量 - int v3 = 0; + String v3 = "局部变量"; + System.out.println(v1); + System.out.println(v2); + System.out.println(v3); + System.out.println(v4); + } + + public static void main(String[] args) { + VariableDemo demo = new VariableDemo(); + demo.test("参数变量"); } } diff --git a/codes/javacore-concurrent/pom.xml b/codes/javacore-concurrent/pom.xml index b748c421..701f38d2 100644 --- a/codes/javacore-concurrent/pom.xml +++ b/codes/javacore-concurrent/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-concurrent - 1.0.1 JavaCore :: Concurrent @@ -42,16 +47,4 @@ spring-core - - - - - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import - - - diff --git a/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/container/ArrayBlockingQueueDemo.java b/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/container/ArrayBlockingQueueDemo.java new file mode 100644 index 00000000..018b3868 --- /dev/null +++ b/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/container/ArrayBlockingQueueDemo.java @@ -0,0 +1,123 @@ +package io.github.dunwu.javacore.concurrent.container; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * BlockingQueue 示例 + * + * @author Zhang Peng + * @date 2024-07-15 + */ +public class ArrayBlockingQueueDemo { + + public static final String EXIT_MSG = "Good bye!"; + + public static void main(String[] args) { + // 使用较小的队列,以更好地在输出中展示其影响 + BlockingQueue 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 queue; + + public Producer(BlockingQueue 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 queue; + + public Consumer(BlockingQueue 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 diff --git a/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/forkjoin/CompletableFutureDemo02.java b/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/forkjoin/CompletableFutureDemo02.java new file mode 100644 index 00000000..40bc7826 --- /dev/null +++ b/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/forkjoin/CompletableFutureDemo02.java @@ -0,0 +1,47 @@ +package io.github.dunwu.javacore.concurrent.forkjoin; + +import java.util.concurrent.CompletableFuture; + +/** + * @author Zhang Peng + * @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()); + } + } + +} diff --git a/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/sync/VolatileDemo.java b/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/sync/VolatileDemo.java new file mode 100644 index 00000000..f4173c38 --- /dev/null +++ b/codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/sync/VolatileDemo.java @@ -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(); + } + +} \ No newline at end of file diff --git a/codes/javacore-container/pom.xml b/codes/javacore-container/pom.xml index 61ab6fed..14f880e1 100644 --- a/codes/javacore-container/pom.xml +++ b/codes/javacore-container/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-container - 1.0.1 JavaCore :: Container Java 容器使用示例 @@ -35,16 +40,4 @@ logback-classic - - - - - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import - - - diff --git "a/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/AsList\347\244\272\344\276\213.java" b/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ArraysAsListDemo.java similarity index 98% rename from "codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/AsList\347\244\272\344\276\213.java" rename to codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ArraysAsListDemo.java index 1e5f5c81..dc1d38cb 100644 --- "a/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/AsList\347\244\272\344\276\213.java" +++ b/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ArraysAsListDemo.java @@ -12,7 +12,7 @@ * @since 2020-08-11 */ @Slf4j -public class AsList示例 { +public class ArraysAsListDemo { public static void main(String[] args) { System.out.println("====================== wrong1 ======================"); diff --git "a/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/SubList\347\244\272\344\276\213.java" b/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ListSubListDemo.java similarity index 85% rename from "codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/SubList\347\244\272\344\276\213.java" rename to codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ListSubListDemo.java index f6e7b26a..b17ddb52 100644 --- "a/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/SubList\347\244\272\344\276\213.java" +++ b/codes/javacore-container/src/main/java/io/github/dunwu/javacore/container/list/ListSubListDemo.java @@ -9,7 +9,7 @@ * @author Zhang Peng * @since 2020-08-11 */ -public class SubList示例 { +public class ListSubListDemo { public static void main(String[] args) throws InterruptedException { // oom(); @@ -19,14 +19,14 @@ public static void main(String[] args) throws InterruptedException { // right2(); } - private static List> data = new ArrayList<>(); +private static List> data = new ArrayList<>(); - private static void oom() { - for (int i = 0; i < 1000; i++) { - List rawList = IntStream.rangeClosed(1, 100000).boxed().collect(Collectors.toList()); - data.add(rawList.subList(0, 1)); - } +private static void oom() { + for (int i = 0; i < 1000; i++) { + List rawList = IntStream.rangeClosed(1, 100000).boxed().collect(Collectors.toList()); + data.add(rawList.subList(0, 1)); } +} private static void oomfix() { for (int i = 0; i < 1000; i++) { diff --git a/codes/javacore-effective/pom.xml b/codes/javacore-effective/pom.xml index bec4a24a..4b3ba3e0 100644 --- a/codes/javacore-effective/pom.xml +++ b/codes/javacore-effective/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-effective - 1.0.1 JavaCore :: Effective @@ -35,16 +40,4 @@ logback-classic - - - - - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import - - - diff --git a/codes/javacore-in-web/pom.xml b/codes/javacore-in-web/pom.xml index f37efe64..a445d858 100644 --- a/codes/javacore-in-web/pom.xml +++ b/codes/javacore-in-web/pom.xml @@ -13,7 +13,7 @@ JavaCore :: Web - 1.7 + 1.8 ${java.version} ${java.version} diff --git a/codes/javacore-io/pom.xml b/codes/javacore-io/pom.xml index 95f06d60..55392850 100644 --- a/codes/javacore-io/pom.xml +++ b/codes/javacore-io/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-io - 1.0.1 JavaCore :: IO @@ -34,16 +39,4 @@ logback-classic - - - - - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import - - - diff --git a/codes/javacore-jdk8/pom.xml b/codes/javacore-jdk8/pom.xml index 2c3cbf33..d65ba4b9 100644 --- a/codes/javacore-jdk8/pom.xml +++ b/codes/javacore-jdk8/pom.xml @@ -3,12 +3,18 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-jdk8 - 1.0.1 JavaCore :: JDK8 + UTF-8 1.8 ${java.version} ${java.version} @@ -34,16 +40,4 @@ hutool-all - - - - - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import - - - diff --git a/codes/javacore-utils/pom.xml b/codes/javacore-utils/pom.xml index 60afbcf5..7a25f555 100644 --- a/codes/javacore-utils/pom.xml +++ b/codes/javacore-utils/pom.xml @@ -3,9 +3,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.github.dunwu.javacore + + io.github.dunwu.javacore + javacore + 1.0.1 + ../../pom.xml + + javacore-utils - 1.0.1 JavaCore :: Utils @@ -38,11 +43,9 @@ - io.github.dunwu - dunwu-dependencies - 1.0.6 - pom - import + org.projectlombok + lombok + 1.18.30 diff --git a/pom.xml b/pom.xml index 6ef2eaad..1758d4b8 100644 --- a/pom.xml +++ b/pom.xml @@ -26,4 +26,32 @@ codes/javacore-in-web codes/bytecode + + + + + cn.hutool + hutool-all + 5.8.29 + + + org.springframework.boot + spring-boot-dependencies + 2.6.2 + pom + import + + + + junit + junit + 4.13.2 + + + org.assertj + assertj-core + 3.26.3 + + +