-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jackliu
committed
Dec 19, 2017
1 parent
ad9da04
commit a8be240
Showing
8 changed files
with
845 additions
and
56 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/cc/ldy/practice/producerconsumer/v1/Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/cc/ldy/practice/producerconsumer/v1/Producer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/cc/ldy/practice/producerconsumer/v1/Queue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/cc/ldy/practice/producerconsumer/v2/Queue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/cc/ldy/practice/producerconsumer/v1/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/cc/ldy/practice/producerconsumer/v2/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |