//一个多生产多消费的多线程通信例子,不知道怎么出问题了?
//生产者-消费者问题俩种方式解决
class Commodity{ //商品类
protected int count=0; //记录仓库商品数量,默认为0
void increase() {count++; //增加一件商品
}
void decrease() {count--; //减少一件商品
}
}
class Produce extends Thread{
private Commodity c;
Produce(Commodity c){this.c=c;}
//当有多个(生产者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while(true) {
while(c.count>=50) { //当数量多于等于50件时,线程休眠,会释放锁
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.increase();
System.out.println("生产线程"+Thread.currentThread().getName()+"生产第"+c.count+"件产品");
this.notifyAll(); //已退出while循环说明仓库未满,恢复第一个休眠的线程
}
}
}
class Consumer extends Thread{
private Commodity c;
Consumer(Commodity c){this.c=c;}
//当有多个(消费者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while(true) {
while(c.count<=0) {
try {
this.wait(); //当仓库已空,当前线程休眠,并释放锁。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.decrease();
System.out.println("消费线程"+Thread.currentThread().getName()+"消费第"+c.count+"件产品");
this.notifyAll();
}
}
}
public static class excise10_11 {
public static void main(String[] args) {
Commodity c=new Commodity();
Produce p1=new Produce(c);
Produce p2=new Produce(c);
Consumer c1=new Consumer(c);
Consumer c2=new Consumer(c);
p1.start();c1.start();c2.start();p2.start();
}
}
//生产者-消费者问题俩种方式解决
class Commodity{ //商品类
protected int count=0; //记录仓库商品数量,默认为0
void increase() {count++; //增加一件商品
}
void decrease() {count--; //减少一件商品
}
}
class Produce extends Thread{
private Commodity c;
Produce(Commodity c){this.c=c;}
//当有多个(生产者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while(true) {
while(c.count>=50) { //当数量多于等于50件时,线程休眠,会释放锁
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.increase();
System.out.println("生产线程"+Thread.currentThread().getName()+"生产第"+c.count+"件产品");
this.notifyAll(); //已退出while循环说明仓库未满,恢复第一个休眠的线程
}
}
}
class Consumer extends Thread{
private Commodity c;
Consumer(Commodity c){this.c=c;}
//当有多个(消费者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while(true) {
while(c.count<=0) {
try {
this.wait(); //当仓库已空,当前线程休眠,并释放锁。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.decrease();
System.out.println("消费线程"+Thread.currentThread().getName()+"消费第"+c.count+"件产品");
this.notifyAll();
}
}
}
public static class excise10_11 {
public static void main(String[] args) {
Commodity c=new Commodity();
Produce p1=new Produce(c);
Produce p2=new Produce(c);
Consumer c1=new Consumer(c);
Consumer c2=new Consumer(c);
p1.start();c1.start();c2.start();p2.start();
}
}
解决方案 »
- ActiveMQ5.8.0 发送BlobMessage出错
- 服务问题
- 求p2p推荐书籍
- 地址栏怎么加密
- "空指针错误 java.lang.NullPointerException "
- 几种开发web service方式(axis,axis2,wsif,xfire)的差别有哪些?
- 求深入浅出hibernate书中的代码
- Java 的Socket有沒有消息机制啊
- 急!!!在线等 达人请进 急!!!
- there are something wrong when i start the J2EE server in my machine, help me, please
- 请问有人知道Tomcat9如何配置log4j?
- Mybatis延迟加载嵌套查询问题
class Commodity { // 商品类
int count = 0; // 记录仓库商品数量,默认为0 void increase() {
count++; // 增加一件商品
} void decrease() {
count--; // 减少一件商品
}
}class Produce extends Thread {
private Commodity c; Produce(Commodity c) {
this.c = c;
} // 当有多个(生产者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while (true) {
while (c.count >= 50) { // 当数量多于等于50件时,线程休眠,会释放锁
try {
System.out.println("");
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.increase();
System.out.println("生产线程" + Thread.currentThread().getName() + "生产第" + c.count + "件产品");
this.notifyAll(); // 已退出while循环说明仓库未满,恢复第一个休眠的线程
}
}
}class Consumer extends Thread {
private Commodity c; Consumer(Commodity c) {
this.c = c;
} // 当有多个(消费者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while (true) {
while (c.count <= 50) {
try {
this.wait(); // 当仓库已空,当前线程休眠,并释放锁。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.decrease();
System.out.println("消费线程" + Thread.currentThread().getName() + "消费第" + c.count + "件产品");
this.notifyAll();
}
}
}public class excise10_11 {
public static void main(String[] args) {
Commodity c = new Commodity();
Produce p1 = new Produce(c);
Produce p2 = new Produce(c);
Consumer c1 = new Consumer(c);
Consumer c2 = new Consumer(c);
p1.start();
c1.start();
c2.start();
p2.start();
}
} 应该是有个括号放错地方了