synchronized +wait/notifAll实现
public class ProductAndConsumer {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Product product = new Product(clerk);
Consumer consumer = new Consumer(clerk);
new Thread(product,"生产者A").start();
new Thread(consumer,"消费者A").start();
new Thread(product,"生产者B").start();
new Thread(consumer,"消费者B").start();
}
}
class Clerk{
private int product=0;
//进货
public synchronized void get() {
//为了防止虚假唤醒,建议用while而不是判断
while(product>=1){
System.out.println("产品已满");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+ ++product);
this.notifyAll();
}
//卖货
public synchronized void set(){
while(product<=0){
System.out.println("产品缺货");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+ --product);
this.notifyAll();
}
}
class Product implements Runnable{
private Clerk clerk;
public Product(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk.get();
}
}
}
class Consumer implements Runnable{
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
clerk.set();
}
}
}
Lock+await/singalAll实现
import java.util.Locale;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProductAndConsumer2 {
public static void main(String[] args) {
Clerk2 clerk2 = new Clerk2();
Product2 product = new Product2(clerk2);
Consumer2 consumer = new Consumer2(clerk2);
new Thread(product,"生产者A").start();
new Thread(consumer,"消费者A").start();
new Thread(product,"生产者B").start();
new Thread(consumer,"消费者B").start();
}
}
class Clerk2{
private int product=0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
//进货
public void get() {
//为了防止虚假唤醒,建议用while而不是判断
lock.lock();
try {
while(product>=1){
System.out.println("产品已满");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+ ++product);
condition.signalAll();
}finally {
lock.unlock();
}
}
//卖货
public synchronized void set(){
lock.lock();
try {
while(product<=0){
System.out.println("产品缺货");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+ --product);
condition.signalAll();
}finally {
lock.unlock();
}
}
}
class Product2 implements Runnable{
private Clerk2 clerk2;
public Product2(Clerk2 clerk2) {
this.clerk2 = clerk2;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk2.get();
}
}
}
class Consumer2 implements Runnable{
private Clerk2 clerk2;
public Consumer2(Clerk2 clerk2) {
this.clerk2 = clerk2;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
clerk2.set();
}
}
}