From dacf5aa2213e82dd2a7cd930497dfa9812f56810 Mon Sep 17 00:00:00 2001 From: hushuai Date: Thu, 16 Apr 2020 16:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E7=A8=8B=E7=BB=83=E4=B9=A0=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../threadtutorial/char06/LockDemo.java | 34 +++++ .../threadtutorial/char06/One2MoreDemo.java | 127 ++++++++++++++++++ .../threadtutorial/char06/SemphoreDemo.java | 33 +++++ .../char06test/LockDemoTest.java | 21 +++ .../char06test/SemphoreDemoTest.java | 21 +++ 5 files changed, 236 insertions(+) create mode 100644 src/main/java/cn/byhieg/threadtutorial/char06/LockDemo.java create mode 100644 src/main/java/cn/byhieg/threadtutorial/char06/One2MoreDemo.java create mode 100644 src/main/java/cn/byhieg/threadtutorial/char06/SemphoreDemo.java create mode 100644 src/test/java/cn/byhieg/threadtutorialtest/char06test/LockDemoTest.java create mode 100644 src/test/java/cn/byhieg/threadtutorialtest/char06test/SemphoreDemoTest.java diff --git a/src/main/java/cn/byhieg/threadtutorial/char06/LockDemo.java b/src/main/java/cn/byhieg/threadtutorial/char06/LockDemo.java new file mode 100644 index 0000000..3b7175f --- /dev/null +++ b/src/main/java/cn/byhieg/threadtutorial/char06/LockDemo.java @@ -0,0 +1,34 @@ +package cn.byhieg.threadtutorial.char06; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @Author: hushuai + * @Date: 2020/4/16 4:11 下午 + * @Description 业务抛出异常需要发送邮件,但一段时间内(5s)不重复发送 + */ +public class LockDemo implements Runnable { + private Lock lock = new ReentrantLock(); + + @Override + public void run() { + try { + throw new Exception(); + } catch (Exception e) { + System.out.println("产生异常..."); + if (lock.tryLock()) { + try { + System.err.println("发送邮件..."); + Thread.sleep(5000L); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } finally { + lock.unlock(); + } + } else { + System.out.println("限定时间内不重复发送邮件"); + } + } + } +} diff --git a/src/main/java/cn/byhieg/threadtutorial/char06/One2MoreDemo.java b/src/main/java/cn/byhieg/threadtutorial/char06/One2MoreDemo.java new file mode 100644 index 0000000..aad0b95 --- /dev/null +++ b/src/main/java/cn/byhieg/threadtutorial/char06/One2MoreDemo.java @@ -0,0 +1,127 @@ +package cn.byhieg.threadtutorial.char06; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * @Author: hushuai + * @Date: 2020/4/16 11:27 上午 + * @Description 三个线程按顺序打印1~100的数字,能被2整除的数字由线程A打印,能被3整除的数字由线程B打印,其他的由线程C打印 + */ +public class One2MoreDemo { + + + public static void main(String[] args) { + BlockingQueue blockingQueue = new ArrayBlockingQueue<>(100); + + new Thread(new Product(blockingQueue)).start(); + new Thread(new CustomeA(blockingQueue), "ThreadA").start(); + new Thread(new CustomeB(blockingQueue), "ThreadB").start(); + new Thread(new CustomeC(blockingQueue), "ThreadC").start(); + } + +} + +/** + * 生产消息 + */ +class Product implements Runnable { + + private BlockingQueue blockingQueue; + + public Product(BlockingQueue blockingQueue) { + this.blockingQueue = blockingQueue; + } + + @Override + public void run() { + for (int i = 1; i <= 100; i++) { + try { + blockingQueue.put(i); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} + +/** + * 打印能被2整除的数字 + */ +class CustomeA implements Runnable { + + private final BlockingQueue blockingQueue; + + public CustomeA(BlockingQueue blockingQueue) { + this.blockingQueue = blockingQueue; + } + + @Override + public void run() { + while (true) { + try { + synchronized (blockingQueue) { + if (blockingQueue.peek() != null && blockingQueue.peek() % 2 == 0) { + System.out.println(Thread.currentThread().getName() + ": " + blockingQueue.take()); + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} + +/** + * 打印能被3整除的数字 + */ +class CustomeB implements Runnable { + + private final BlockingQueue blockingQueue; + + public CustomeB(BlockingQueue blockingQueue) { + this.blockingQueue = blockingQueue; + } + + @Override + public void run() { + while (true) { + try { + synchronized (blockingQueue) { + if (blockingQueue.peek() != null && blockingQueue.peek() % 3 == 0) { + System.out.println(Thread.currentThread().getName() + ": " + blockingQueue.take()); + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} + +/** + * 打印不能被2或3整除的数字 + */ +class CustomeC implements Runnable { + + private final BlockingQueue blockingQueue; + + public CustomeC(BlockingQueue blockingQueue) { + this.blockingQueue = blockingQueue; + } + + @Override + public void run() { + while (true) { + try { + synchronized (blockingQueue) { + if (blockingQueue.peek() != null && blockingQueue.peek() % 2 != 0 && blockingQueue.peek() % 3 != 0) { + System.out.println(Thread.currentThread().getName() + ": " + blockingQueue.take()); + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/cn/byhieg/threadtutorial/char06/SemphoreDemo.java b/src/main/java/cn/byhieg/threadtutorial/char06/SemphoreDemo.java new file mode 100644 index 0000000..1da9cb1 --- /dev/null +++ b/src/main/java/cn/byhieg/threadtutorial/char06/SemphoreDemo.java @@ -0,0 +1,33 @@ +package cn.byhieg.threadtutorial.char06; + +import java.util.Date; +import java.util.concurrent.Semaphore; + +/** + * @Author: hushuai + * @Date: 2020/4/16 4:12 下午 + * @Description 业务抛出异常需要发送邮件,但一段时间内(5s)最多发送3封邮件 + */ +public class SemphoreDemo implements Runnable { + private final Semaphore semaphore = new Semaphore(3); + + @Override + public void run() { + try { + throw new Exception(); + } catch (Exception e) { + if (semaphore.tryAcquire()) { + try { + System.err.println(new Date() + "发送邮件..."); + Thread.sleep(5000L); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + semaphore.release(); + } + } else { + System.out.println("限定时间内发送邮件数量达到上限"); + } + } + } +} diff --git a/src/test/java/cn/byhieg/threadtutorialtest/char06test/LockDemoTest.java b/src/test/java/cn/byhieg/threadtutorialtest/char06test/LockDemoTest.java new file mode 100644 index 0000000..74cc591 --- /dev/null +++ b/src/test/java/cn/byhieg/threadtutorialtest/char06test/LockDemoTest.java @@ -0,0 +1,21 @@ +package cn.byhieg.threadtutorialtest.char06test; + +import cn.byhieg.threadtutorial.char06.LockDemo; +import org.junit.Test; + +/** + * @Author: hushuai + * @Date: 2020/4/16 3:03 下午 + * @Description + */ +public class LockDemoTest { + + @Test + public void lockDemo() throws InterruptedException { + LockDemo demo = new LockDemo(); + for (int i = 0; i < 50; i++) { + new Thread(demo).start(); + Thread.sleep(1000L); + } + } +} diff --git a/src/test/java/cn/byhieg/threadtutorialtest/char06test/SemphoreDemoTest.java b/src/test/java/cn/byhieg/threadtutorialtest/char06test/SemphoreDemoTest.java new file mode 100644 index 0000000..9df0826 --- /dev/null +++ b/src/test/java/cn/byhieg/threadtutorialtest/char06test/SemphoreDemoTest.java @@ -0,0 +1,21 @@ +package cn.byhieg.threadtutorialtest.char06test; + +import cn.byhieg.threadtutorial.char06.SemphoreDemo; +import org.junit.Test; + +/** + * @Author: hushuai + * @Date: 2020/4/16 3:03 下午 + * @Description + */ +public class SemphoreDemoTest { + + @Test + public void lockDemo() throws InterruptedException { + SemphoreDemo demo = new SemphoreDemo(); + for (int i = 0; i < 50; i++) { + new Thread(demo).start(); + Thread.sleep(1000L); + } + } +}