From a8be24042428324d1efed56a3a575641178e97a8 Mon Sep 17 00:00:00 2001 From: jackliu Date: Tue, 19 Dec 2017 14:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E4=BA=A7=E8=80=85=E6=B6=88=E8=B4=B9?= =?UTF-8?q?=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 643 ++++++++++++++++-- .../ldy/practice/producerconsumer/IQueue.java | 9 + .../producerconsumer/v1/Consumer.java | 26 + .../producerconsumer/v1/Producer.java | 29 + .../practice/producerconsumer/v1/Queue.java | 71 ++ .../practice/producerconsumer/v2/Queue.java | 74 ++ .../practice/producerconsumer/v1/Test.java | 25 + .../practice/producerconsumer/v2/Test.java | 24 + 8 files changed, 845 insertions(+), 56 deletions(-) create mode 100644 src/main/java/cc/ldy/practice/producerconsumer/IQueue.java create mode 100644 src/main/java/cc/ldy/practice/producerconsumer/v1/Consumer.java create mode 100644 src/main/java/cc/ldy/practice/producerconsumer/v1/Producer.java create mode 100644 src/main/java/cc/ldy/practice/producerconsumer/v1/Queue.java create mode 100644 src/main/java/cc/ldy/practice/producerconsumer/v2/Queue.java create mode 100644 src/test/java/cc/ldy/practice/producerconsumer/v1/Test.java create mode 100644 src/test/java/cc/ldy/practice/producerconsumer/v2/Test.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8976fa9..048eb61 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,14 @@ - - + + + + + + + + @@ -17,16 +23,6 @@ - - - - - - - - - - @@ -40,11 +36,86 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -53,8 +124,8 @@ - - + + @@ -66,6 +137,7 @@ @@ -85,6 +157,13 @@ @@ -102,8 +181,8 @@ - @@ -123,8 +202,6 @@ - - @@ -164,6 +241,118 @@ + + @@ -220,11 +431,17 @@ + + + + + + - + @@ -265,6 +482,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + - - + + + + + @@ -723,35 +1024,59 @@ - + + + + + + + 1512521516963 + + - - + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + + + + - - + + @@ -759,15 +1084,15 @@ - + - - + + - + @@ -779,7 +1104,7 @@ - + @@ -794,6 +1119,8 @@ + + @@ -804,6 +1131,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -835,7 +1288,10 @@ - + + + + @@ -870,7 +1326,10 @@ - + + + + @@ -897,6 +1356,17 @@ + + + + + + + + + + + @@ -912,30 +1382,91 @@ - + - - - + + + + + + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/cc/ldy/practice/producerconsumer/IQueue.java b/src/main/java/cc/ldy/practice/producerconsumer/IQueue.java new file mode 100644 index 0000000..84638f2 --- /dev/null +++ b/src/main/java/cc/ldy/practice/producerconsumer/IQueue.java @@ -0,0 +1,9 @@ +package cc.ldy.practice.producerconsumer; + +/** + * Created by ldy on 2017/12/15. + */ +public interface IQueue { + void write(int obj); + int read(); +} diff --git a/src/main/java/cc/ldy/practice/producerconsumer/v1/Consumer.java b/src/main/java/cc/ldy/practice/producerconsumer/v1/Consumer.java new file mode 100644 index 0000000..e92a6ae --- /dev/null +++ b/src/main/java/cc/ldy/practice/producerconsumer/v1/Consumer.java @@ -0,0 +1,26 @@ +package cc.ldy.practice.producerconsumer.v1; + +import cc.ldy.practice.producerconsumer.IQueue; + +/** + * Created by ldy on 2017/12/15. + */ +public class Consumer extends Thread{ + private IQueue queue; + + public Consumer(IQueue queue) { + this.queue = queue; + } + + public void run() { + if (queue == null) { + throw new RuntimeException("null queue"); + } + + int i = 0; + while (i++ < 100) { + queue.read(); + } + } + +} diff --git a/src/main/java/cc/ldy/practice/producerconsumer/v1/Producer.java b/src/main/java/cc/ldy/practice/producerconsumer/v1/Producer.java new file mode 100644 index 0000000..a9780ca --- /dev/null +++ b/src/main/java/cc/ldy/practice/producerconsumer/v1/Producer.java @@ -0,0 +1,29 @@ +package cc.ldy.practice.producerconsumer.v1; + +import cc.ldy.practice.producerconsumer.IQueue; + +import java.util.Random; + +/** + * Created by ldy on 2017/12/15. + */ +public class Producer extends Thread { + private IQueue queue; + private Random random = new Random(); + + public Producer(IQueue queue) { + this.queue = queue; + } + + public void run() { + if (queue == null) { + throw new RuntimeException("null queue"); + } + + int i = 0; + while (i++ < 100) { + queue.write(i); + } + } + +} diff --git a/src/main/java/cc/ldy/practice/producerconsumer/v1/Queue.java b/src/main/java/cc/ldy/practice/producerconsumer/v1/Queue.java new file mode 100644 index 0000000..7a83754 --- /dev/null +++ b/src/main/java/cc/ldy/practice/producerconsumer/v1/Queue.java @@ -0,0 +1,71 @@ +package cc.ldy.practice.producerconsumer.v1; + +import cc.ldy.practice.producerconsumer.IQueue; + +/** + * Created by ldy on 2017/12/15. + */ +public class Queue implements IQueue { + private int[] a = new int[10]; + private int count;//当前队列中的元素数 + private int writeIdx; + private int readIdx; + + public void write(int obj) { + synchronized (this) { + while (count >= a.length) { + try { + this.wait(); + } catch (InterruptedException e) { + } + } + + if (writeIdx == a.length) { + writeIdx = 0; + } + + a[writeIdx] = obj; + writeIdx++; + count++; + System.out.println("write count:"+count); + System.out.println("write:"+obj); + print(); + + this.notifyAll(); + + } + } + + public int read() { + synchronized (this) { + while (count <= 0) { + try { + this.wait(); + } catch (InterruptedException e) { + } + } + + if (readIdx == a.length) { + readIdx = 0; + } + + int ret = a[readIdx]; + readIdx++; + count--; + + System.out.println("read count:"+count); + System.out.println("read:"+ret); + print(); + notifyAll(); + + return ret; + } + } + + private void print() { + for (int i :a) { + System.out.print("" + i + " "); + } + System.out.println(); + } +} diff --git a/src/main/java/cc/ldy/practice/producerconsumer/v2/Queue.java b/src/main/java/cc/ldy/practice/producerconsumer/v2/Queue.java new file mode 100644 index 0000000..91d661f --- /dev/null +++ b/src/main/java/cc/ldy/practice/producerconsumer/v2/Queue.java @@ -0,0 +1,74 @@ +package cc.ldy.practice.producerconsumer.v2; + +import cc.ldy.practice.producerconsumer.IQueue; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * Created by ldy on 2017/12/15. + */ +public class Queue implements IQueue{ + int [] a = new int[10]; + ReentrantLock lock = new ReentrantLock(); + Condition writeCondition = lock.newCondition(); + Condition readCondition = lock.newCondition(); + + private int count;//当前队列中的元素数 + private int writeIdx; + private int readIdx; + + public void write(int obj) { + lock.lock(); + try { + while (count >= a.length) { + try { + writeCondition.await(); + } catch (InterruptedException e) { + + } + } + + if (writeIdx == a.length) { + writeIdx = 0; + } + + a[writeIdx] = obj; + count++; + writeIdx++; + System.out.println("write count:"+count); + System.out.println("write:"+obj); + readCondition.signalAll(); + } finally { + lock.unlock(); + } + } + + public int read() { + lock.lock(); + int ret; + try { + while (count <= 0) { + try { + readCondition.await(); + } catch (InterruptedException e) { + + } + } + if (readIdx >= a.length) { + readIdx = 0; + } + + ret = a[readIdx]; + readIdx++; + count--; + System.out.println("read count:"+count); + System.out.println("read:"+ret); + writeCondition.signalAll(); + } finally { + lock.unlock(); + } + + return ret; + } +} diff --git a/src/test/java/cc/ldy/practice/producerconsumer/v1/Test.java b/src/test/java/cc/ldy/practice/producerconsumer/v1/Test.java new file mode 100644 index 0000000..88085e3 --- /dev/null +++ b/src/test/java/cc/ldy/practice/producerconsumer/v1/Test.java @@ -0,0 +1,25 @@ +package cc.ldy.practice.producerconsumer.v1; + + +/** + * Created by ldy on 2017/12/15. + */ +public class Test { + @org.junit.Test + public void test() throws Exception { + Queue q = new Queue(); + + Producer producer = new Producer(q); + Consumer consumer = new Consumer(q); + + + + producer.start(); + consumer.start(); + + producer.join(); + consumer.join(); + + + } +} diff --git a/src/test/java/cc/ldy/practice/producerconsumer/v2/Test.java b/src/test/java/cc/ldy/practice/producerconsumer/v2/Test.java new file mode 100644 index 0000000..fbbdaeb --- /dev/null +++ b/src/test/java/cc/ldy/practice/producerconsumer/v2/Test.java @@ -0,0 +1,24 @@ +package cc.ldy.practice.producerconsumer.v2; + + +import cc.ldy.practice.producerconsumer.v1.Consumer; +import cc.ldy.practice.producerconsumer.v1.Producer; + +/** + * Created by ldy on 2017/12/15. + */ +public class Test { + @org.junit.Test + public void test() throws Exception { + Queue q = new Queue(); + + Producer producer = new Producer(q); + Consumer consumer = new Consumer(q); + + producer.start(); + consumer.start(); + + producer.join(); + consumer.join(); + } +}