From 8228a4aae3448f59507e2e848b196011c630f77c Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Mon, 5 Aug 2024 23:04:23 +0800 Subject: [PATCH] =?UTF-8?q?"threading=20examples:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E5=99=A8=E3=80=81=E6=A0=85=E6=A0=8F=E5=92=8C?= =?UTF-8?q?=E6=9C=AA=E6=9D=A5=E6=BC=94=E7=A4=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加三个新的演示类来展示不同线程控制机制的使用:- CountDownLatchDemo2:展示如何使用CountDownLatch来同步等待其他线程完成。 - CyclicBarrierDemo2:展示如何使用CyclicBarrier来周期性地等待一组线程完成。 - FutureGetDemo:展示如何使用ExecutorService和Future来异步执行任务并等待它们完成。这些演示类补充了现有的线程演示,提供了更多有关如何在Java中使用高级线程控制结构的实例。" ```添加动态规划解决方案和新的测试用例为特定问题实现动态规划解决方案,并添加新的测试用例以验证代码的正确性。具体包括: 1. 添加LeetCode 45题的两个解:jump和jump1,使用不同的动态规划策略。 2. 添加LeetCode 53题的两个解:使用动态规划的Solution1和Solution2。 3. 添加LeetCode55题的解:使用动态规划的Solution。4. 添加LeetCode 122题的两个解:maxProfit和maxProfit1,分别使用不同的策略。 5. 添加LeetCode376题的解:使用动态规划的Solution。 所有添加的解决方案都通过了相应的测试用例,确保了代码的正确性和有效性。 ``` --- .../java/cn/whaifree/test/ThreadDemo1.java | 135 ++++++++++++++---- 1 file changed, 105 insertions(+), 30 deletions(-) diff --git a/src/main/java/cn/whaifree/test/ThreadDemo1.java b/src/main/java/cn/whaifree/test/ThreadDemo1.java index fee847f..f864e29 100644 --- a/src/main/java/cn/whaifree/test/ThreadDemo1.java +++ b/src/main/java/cn/whaifree/test/ThreadDemo1.java @@ -1,44 +1,119 @@ package cn.whaifree.test; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; + public class ThreadDemo1 { static Object lock = new Object(); static int num = 0; - public static void main(String[] args) { - new Thread(() -> { - for (int i = 0; i < 50; i++) { - synchronized (lock){ - while (num%2!=0){ - try { - lock.wait(); // wait会释放锁 - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - num++; - System.out.println("a"); - lock.notifyAll(); - } +// public static void main(String[] args) { +// new Thread(() -> { +// for (int i = 0; i < 50; i++) { +// synchronized (lock){ +// while (num%2!=0){ +// try { +// lock.wait(); // wait会释放锁 +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } +// } +// num++; +// System.out.println("a"); +// lock.notifyAll(); +// } +// } +// +// }).start(); +// +// new Thread(() -> { +// for (int i = 0; i < 50; i++) { +// synchronized (lock){ // wait释放锁后这个线程就能拿到锁 +// while (num%2!=1){ +// try { +// lock.wait(); +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } +// } +// num++; +// System.out.println("b"); +// lock.notifyAll(); +// } +// } +// }).start(); +// } + + static class CountDownLatchDemo2{ + + public static void main(String[] args) throws InterruptedException { + + AtomicInteger num = new AtomicInteger(9); + CountDownLatch countDownLatch = new CountDownLatch(num.get()); + + for (int i = 0; i < 9; i++) { + int finalI = i; + new Thread(() -> { + System.out.println(Thread.currentThread().getName() + " " + finalI); + countDownLatch.countDown(); + }).start(); } - }).start(); + countDownLatch.await(); - new Thread(() -> { - for (int i = 0; i < 50; i++) { - synchronized (lock){ // wait释放锁后这个线程就能拿到锁 - while (num%2!=1){ - try { - lock.wait(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + new Thread(() -> System.out.println("上面的9个执行完了,轮到我了 wait complete!")).start(); + + } + } + + static class CyclicBarrierDemo2{ + + public static void main(String[] args) throws InterruptedException { + + // 每9个cyclicBarrier.await()执行后,都执行一次wait + CyclicBarrier cyclicBarrier = new CyclicBarrier(9, + () -> System.out.println("上面的9个执行完了,轮到我了 wait complete!") // 等待前9次执行结束 + ); + + + for (int i = 0; i < 9; i++) { + int finalI = i; + new Thread(() -> { + try { + System.out.println(Thread.currentThread().getName() + " " + finalI); + cyclicBarrier.await(); + } catch (Exception e) { + throw new RuntimeException(e); } - num++; - System.out.println("b"); - lock.notifyAll(); - } + }).start(); } - }).start(); + + } + } + + static class FutureGetDemo{ + public static void main(String[] args) throws ExecutionException, InterruptedException { + ExecutorService executorService = Executors.newFixedThreadPool(4); // 设置4,4个核心线程 + List> futures = new ArrayList<>(); + for (int i = 0; i < 9; i++) { + int finalI = i; + Future submit = executorService.submit(() -> { + Thread.sleep(1000); + System.out.println(Thread.currentThread().getName() + " " + finalI); + return null; + }); + futures.add(submit); + } + + for (Future future : futures) { + future.get(); + } + + System.out.println("上面的9个执行完了,轮到我了 wait complete!"); + executorService.shutdown(); + } } }