通过wait()和 notifyAll()实现
public class PublicQueue {
public int putindex = 0;
public int maxcount = 10;
public synchronized void add(){
while(putindex >= maxcount){
try {
System.out.println("没有东西可以加了");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notifyAll();
putindex++;
System.out.println(Thread.currentThread().getName() + " 生产加1变成:"+putindex);
}
public synchronized void remove(){
while(putindex == 0) {
try {
System.out.println("没有东西可以减了");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notifyAll();
putindex--;
System.out.println(Thread.currentThread().getName()+" 消费减1:"+putindex );
}
}
public class Consumer implements Runnable {
private PublicQueue publicQueue;
public Consumer(PublicQueue publicQueue){
this.publicQueue = publicQueue;
}
@Override
public void run(){
while (true){
try {
Thread.sleep(3000);
publicQueue.remove();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Producer implements Runnable {
private PublicQueue publicQueue;
public Producer(PublicQueue publicQueue){
this.publicQueue = publicQueue;
}
@Override
public void run(){
while (true){
try {
Thread.sleep(1000);
publicQueue.add();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
PublicQueue publicQueue = new PublicQueue();
Thread p1 = new Thread(new Producer(publicQueue), "produce1");
Thread p2 = new Thread(new Producer(publicQueue), "produce2");
Thread p3 = new Thread(new Producer(publicQueue), "produce3");
Thread s1 = new Thread(new Consumer(publicQueue), "consumer1");
Thread s2 = new Thread(new Consumer(publicQueue), "consumer2");
Thread s3 = new Thread(new Consumer(publicQueue), "consumer3");
p1.start();
p2.start();
p3.start();
s1.start();
s2.start();
s3.start();
}
BlockingQueue 实现
public class BlockingQueueTest {
public static int count = 0;
public BlockingQueue blockingQueue = new ArrayBlockingQueue(10);
public static void main(String[] args) {
BlockingQueueTest test3 = new BlockingQueueTest();
new Thread(test3.new ProducerTest()).start();
new Thread(test3.new ConsumerTest()).start();
new Thread(test3.new ProducerTest()).start();
new Thread(test3.new ConsumerTest()).start();
new Thread(test3.new ProducerTest()).start();
new Thread(test3.new ConsumerTest()).start();
new Thread(test3.new ProducerTest()).start();
new Thread(test3.new ConsumerTest()).start();
}
class ProducerTest implements Runnable{
@Override
public void run() {
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
blockingQueue.put(1);//add会抛出异常 put会阻塞
count++;
System.out.println(Thread.currentThread().getName()
+ "生产者生产,目前总共有" + count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class ConsumerTest implements Runnable{
@Override
public void run() {
while (true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
blockingQueue.take();
count--;
System.out.println(Thread.currentThread().getName()
+ "消费者消费,目前总共有" + count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}