生产者--消费者package two;public class Goods {
//当前产品编号
private static int num;
//当前产品是否已经存在
private boolean isBeing = false; public boolean isBeing() {
return isBeing;
} public void setBeing(boolean isBeing) {
this.isBeing = isBeing;
}
public void setNum() {
num++;
}
public int getNum() {
return num;
}
public void product() {
while (isBeing()) {
try {
wait();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
setNum();
System.out.println("第" + getNum() + "个产品已生产...");
setBeing(true);
notifyAll();
}
public void consume() {
while (!isBeing()) {
try {
wait();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
System.out.println("第" + getNum() + "个产品已消费...");
setBeing(false);
notifyAll();
}
}
package two;public class Productor extends Thread{
Goods good;
public Productor(Goods good) {
this.good = good;
}
public void run() {
for (int i=0; i<10; i++) {
good.product();
}
}
}
package two;public class Consumer extends Thread{
Goods good;
public Consumer(Goods good) {
this.good = good;
}
public void run() {
for (int i=0; i<10; i++) {
good.consume();
}
}
}package two;public class TestGoods {
public static void main(String[] args) {
Goods good = new Goods();
Productor pro = new Productor(good);
Consumer con = new Consumer(good);
pro.start();
con.start();
}
}多线程糊涂了...为什么抛出IllegalMonitorStateException异常?谁详细解释下这方面的问题啊...
//当前产品编号
private static int num;
//当前产品是否已经存在
private boolean isBeing = false; public boolean isBeing() {
return isBeing;
} public void setBeing(boolean isBeing) {
this.isBeing = isBeing;
}
public void setNum() {
num++;
}
public int getNum() {
return num;
}
public void product() {
while (isBeing()) {
try {
wait();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
setNum();
System.out.println("第" + getNum() + "个产品已生产...");
setBeing(true);
notifyAll();
}
public void consume() {
while (!isBeing()) {
try {
wait();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
System.out.println("第" + getNum() + "个产品已消费...");
setBeing(false);
notifyAll();
}
}
package two;public class Productor extends Thread{
Goods good;
public Productor(Goods good) {
this.good = good;
}
public void run() {
for (int i=0; i<10; i++) {
good.product();
}
}
}
package two;public class Consumer extends Thread{
Goods good;
public Consumer(Goods good) {
this.good = good;
}
public void run() {
for (int i=0; i<10; i++) {
good.consume();
}
}
}package two;public class TestGoods {
public static void main(String[] args) {
Goods good = new Goods();
Productor pro = new Productor(good);
Consumer con = new Consumer(good);
pro.start();
con.start();
}
}多线程糊涂了...为什么抛出IllegalMonitorStateException异常?谁详细解释下这方面的问题啊...
synch...
wait()
notify()
allnotify()
用错了,为什么,我不知道
发生原因:使用wait或notify,notifyall方法时,没有对对象加锁。
解决办法:加锁
public synchronized void product() {
while (isBeing()) {
try {
wait();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
setNum();
System.out.println("第" + getNum() + "个产品已生产...");
setBeing(true);
notifyAll();
}synchronized 是必须的啊,wait(),notify(),notifyAll()都必须在同步方法或者同步块中使用,即是必须加锁。
线程同步的问题product方法和consume方法都加上synchronized同步就可以了