JAVA多线程生产者与消费者问题
昨天看了一下资料,模仿着写了个,怎么跑起来死锁了?望各位大牛看看哪代码有误 ?
正确的情况 应该是 ,打印 生产 一个 ,接着打印 消费一个 (假设最多只能存放一个产品)
/**
* 店员类
* 假设 店内只有一个产品架 只能存放一个产品
*
*
*/
public class Clerk {
//-1代表店内没有可用产品
private int product = -1;
//生产一个产品
public synchronized void setProduct(int product) throws InterruptedException{
if(this.product != -1){
//产品架上还有产品 等待消费者消费
wait();
}
this.product = product;
System.out.println("当前线程"+Thread.currentThread().getName()+"生产第"+product+"个产品");
//解锁 并通知其他线程进入线程等待队列排队
notify();
}
//消费一个产品
public synchronized void getProduct(int product) {
if(this.product == -1){
//没有产品 等待生产者生产
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.product = -1;
System.out.println("当前线程"+Thread.currentThread().getName()+"消费第"+product+"个产品");
//解锁 并通知其他线程进入线程等待队列排队
notify();
}
}
/**
* 生产者
*
*
*/
public class Produce implements Runnable {
private Clerk clerk;
public void run(){
for(int i=1; i<=10; i++){
if(this.clerk == null){
clerk = new Clerk();
}
System.out.println("----当前线程"+Thread.currentThread().getName()+ "开始生产");
try {
clerk.setProduct(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费者
*
*
*/
public class Consume implements Runnable {
private Clerk clerk;
public void run(){
for(int i=1; i<=10; i++){
if(this.clerk == null){
clerk = new Clerk();
}
System.out.println("----当前线程"+Thread.currentThread().getName()+ "开始消费");
try {
clerk.getProduct(i);
} catch (Exception e) {
e.printStackTrace();
};
}
}
}
//调用类
public class Main {
public static void main(String[] args){
Produce pro = new Produce();
Thread produceThread = new Thread(pro);
produceThread.start();
Consume con = new Consume();
Thread consumeThread = new Thread(con);
consumeThread.start();
}
}
昨天看了一下资料,模仿着写了个,怎么跑起来死锁了?望各位大牛看看哪代码有误 ?
正确的情况 应该是 ,打印 生产 一个 ,接着打印 消费一个 (假设最多只能存放一个产品)
/**
* 店员类
* 假设 店内只有一个产品架 只能存放一个产品
*
*
*/
public class Clerk {
//-1代表店内没有可用产品
private int product = -1;
//生产一个产品
public synchronized void setProduct(int product) throws InterruptedException{
if(this.product != -1){
//产品架上还有产品 等待消费者消费
wait();
}
this.product = product;
System.out.println("当前线程"+Thread.currentThread().getName()+"生产第"+product+"个产品");
//解锁 并通知其他线程进入线程等待队列排队
notify();
}
//消费一个产品
public synchronized void getProduct(int product) {
if(this.product == -1){
//没有产品 等待生产者生产
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.product = -1;
System.out.println("当前线程"+Thread.currentThread().getName()+"消费第"+product+"个产品");
//解锁 并通知其他线程进入线程等待队列排队
notify();
}
}
/**
* 生产者
*
*
*/
public class Produce implements Runnable {
private Clerk clerk;
public void run(){
for(int i=1; i<=10; i++){
if(this.clerk == null){
clerk = new Clerk();
}
System.out.println("----当前线程"+Thread.currentThread().getName()+ "开始生产");
try {
clerk.setProduct(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费者
*
*
*/
public class Consume implements Runnable {
private Clerk clerk;
public void run(){
for(int i=1; i<=10; i++){
if(this.clerk == null){
clerk = new Clerk();
}
System.out.println("----当前线程"+Thread.currentThread().getName()+ "开始消费");
try {
clerk.getProduct(i);
} catch (Exception e) {
e.printStackTrace();
};
}
}
}
//调用类
public class Main {
public static void main(String[] args){
Produce pro = new Produce();
Thread produceThread = new Thread(pro);
produceThread.start();
Consume con = new Consume();
Thread consumeThread = new Thread(con);
consumeThread.start();
}
}