Skip to content

Commit

Permalink
生产者消费者
Browse files Browse the repository at this point in the history
  • Loading branch information
jackliu committed Dec 19, 2017
1 parent ad9da04 commit a8be240
Show file tree
Hide file tree
Showing 8 changed files with 845 additions and 56 deletions.
643 changes: 587 additions & 56 deletions .idea/workspace.xml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/main/java/cc/ldy/practice/producerconsumer/IQueue.java
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 src/main/java/cc/ldy/practice/producerconsumer/v1/Consumer.java
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 src/main/java/cc/ldy/practice/producerconsumer/v1/Producer.java
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 src/main/java/cc/ldy/practice/producerconsumer/v1/Queue.java
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 src/main/java/cc/ldy/practice/producerconsumer/v2/Queue.java
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 src/test/java/cc/ldy/practice/producerconsumer/v1/Test.java
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 src/test/java/cc/ldy/practice/producerconsumer/v2/Test.java
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();
}
}

0 comments on commit a8be240

Please sign in to comment.