From 4cfec70118a964024c9be72e01efadc26cf4171a Mon Sep 17 00:00:00 2001 From: ex_kongxiang Date: Thu, 26 Oct 2023 11:00:47 +0800 Subject: [PATCH] =?UTF-8?q?feat=20:=20=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/CountDownLatchTest.java | 31 ++++++++++++++++++++++++++ test/CyclicBarrierTest.java | 43 ++++++++++++++++++++++++++++++++++++ test/ExchangerTest.java | 41 ++++++++++++++++++++++++++++++++++ test/SemaphoreTest.java | 42 +++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 test/CountDownLatchTest.java create mode 100644 test/CyclicBarrierTest.java create mode 100644 test/ExchangerTest.java create mode 100644 test/SemaphoreTest.java diff --git a/test/CountDownLatchTest.java b/test/CountDownLatchTest.java new file mode 100644 index 0000000..aaa6924 --- /dev/null +++ b/test/CountDownLatchTest.java @@ -0,0 +1,31 @@ +import org.testng.annotations.Test; + +import java.util.concurrent.CountDownLatch; + +/** + * @author 孔翔 + * @since 2023-10-25 + * copyright for author : 孔翔 at 2023-10-25 + * java-study + */ +public class CountDownLatchTest { + @Test + public void test() throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(5); + + SimpleThreadUtils.newLoopThread(5, "cd", 1, (t) -> { + int i = t.getNo() * 1000; + try { + Thread.sleep(i); + } catch (InterruptedException e) { + } + System.out.println("加载资源线程:"+t.getName()+": 花费了 " + i + "s"); + countDownLatch.countDown(); + }); + + countDownLatch.await(); + + + System.out.println("开始游戏"); + } +} diff --git a/test/CyclicBarrierTest.java b/test/CyclicBarrierTest.java new file mode 100644 index 0000000..7b5bf1d --- /dev/null +++ b/test/CyclicBarrierTest.java @@ -0,0 +1,43 @@ +import org.testng.annotations.Test; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; + +/** + * @author 孔翔 + * @since 2023-10-25 + * copyright for author : 孔翔 at 2023-10-25 + * java-study + */ +public class CyclicBarrierTest { + + @Test + public void test() throws InterruptedException { + CyclicBarrier cyclicBarrier = new CyclicBarrier(5); + + SimpleThreadUtils.newLoopThread(5, "cd", 1, (t) -> { + while (true) { + int i = t.getNo() * 1000; + try { + Thread.sleep(i); + } catch (InterruptedException e) { + } + System.out.println(":" + t.getName() + ": 准备好了"); + try { + cyclicBarrier.await(); + System.out.println(" 冲。。。 :" + t.getName()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (BrokenBarrierException e) { + throw new RuntimeException(e); + } + } + }); + + + SimpleThreadUtils.wait(10000,()->{}); + + } + +} diff --git a/test/ExchangerTest.java b/test/ExchangerTest.java new file mode 100644 index 0000000..b194408 --- /dev/null +++ b/test/ExchangerTest.java @@ -0,0 +1,41 @@ +import org.testng.annotations.Test; + +import java.util.concurrent.Exchanger; + +/** + * @author 孔翔 + * @since 2023-10-25 + * copyright for author : 孔翔 at 2023-10-25 + * java-study + */ +public class ExchangerTest { + + + @Test + public void test() { + Exchanger exchanger = new Exchanger<>(); + + SimpleThreadUtils.newLoopThread(2,"ex",2,(t)->{ + if (t.getNo() == 0){ + try { + String exchange = exchanger.exchange("我先到了,你死定了"); + System.out.println(t.getName()+":"+exchange); + } catch (InterruptedException e) { + } + }else { + try { + Thread.sleep(5000); + String exchange = exchanger.exchange("先到了吧,去死吧"); + System.out.println(t.getName()+":"+exchange); + + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + } + + }); + + SimpleThreadUtils.wait(1000,()->{}); + } +} diff --git a/test/SemaphoreTest.java b/test/SemaphoreTest.java new file mode 100644 index 0000000..1221198 --- /dev/null +++ b/test/SemaphoreTest.java @@ -0,0 +1,42 @@ +import org.testng.annotations.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; + +/** + * @author 孔翔 + * @since 2023-10-25 + * copyright for author : 孔翔 at 2023-10-25 + * java-study + */ +public class SemaphoreTest { + @Test + public void test() throws InterruptedException { + Semaphore semaphore = new Semaphore(5); + + SimpleThreadUtils.newLoopThread(10, "cd", 10, (t) -> { + try { + semaphore.acquire(); + + int i = t.getNo() * 1000; + try { + Thread.sleep(i); + } catch (InterruptedException e) { + } + System.out.println("加载资源线程:"+t.getName()+": 花费了 " + i + "s"); + + } catch (InterruptedException e) { + throw new RuntimeException(e); + }finally { + semaphore.release(); + } + + + }); + + + + + SimpleThreadUtils.wait(10000,()->{}); + } +} -- GitLab