黑马程序员技术交流社区
标题:
这个程序为什么会报IllegalMonitorStateException
[打印本页]
作者:
梁清平
时间:
2012-5-28 12:53
标题:
这个程序为什么会报IllegalMonitorStateException
本帖最后由 梁清平 于 2012-5-28 12:54 编辑
这个程序不能运行还有哪些问题啊?请高手指点。。
//生产者和消费者的示例
import java.util.concurrent.locks .*;
//主线程
public class ConsumerProducerDemo
{
public static void main(String[] args)
{
Products p = new Products();
new Thread(new Producer(p)).start();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
new Thread(new Consumer(p)).start();
}
}
//定义一个产品类
class Products
{
private ReentrantLock lock = new ReentrantLock();
Condition condition_pro = lock.newCondition();
Condition condition_con = lock.newCondition();
private String name;
private int count;
private boolean flag;
public void input(String name)
{
lock.lock();
try
{
while(flag)
{
try
{
condition_pro.await();
}
catch(InterruptedException e)
{
System.out.println("线程唤醒异常!");
}
}
this.name = name+"---:"+count++;
System.out.println(Thread.currentThread().getName()+"..."+this.name);
}
finally
{
lock.unlock();
}
flag = true;
condition_con.signal();
}
public void output()
{
lock.lock();
try
{
while(!flag)
try
{
condition_con.await();
}
catch(InterruptedException e)
{
System.out.println("线程唤醒异常!");
}
System.out.println(Thread.currentThread().getName()+"!!!!!!"+this.name);
}
finally
{
lock.unlock();
}
flag = false;
condition_pro.signal();
}
}
//定义两个线程类
class Producer implements Runnable
{
private Products p;
Producer(Products p)
{
this.p = p;
}
public void run()
{
while(true)
p.input("产品");
}
}
class Consumer implements Runnable
{
private Products p;
Consumer(Products p)
{
this.p = p;
}
public void run()
{
while(true)
p.output();
}
}
作者:
黑马-唐磊
时间:
2012-5-28 13:14
本帖最后由 黑马-唐磊 于 2012-5-28 13:25 编辑
你那个标记唤醒出了问题
flag=true;
condition_con.signal();你定义在finally之后,当finally一执行后面的代码就不执行了,也就不会唤醒,所以出现了非法监视状态异常。我给你修改了一下,下面就能正常运行
import java.util.concurrent.locks .*;
//主线程
public class Demo10
{
public static void main(String[] args)
{
Products p = new Products();
new Thread(new Producer(p)).start();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
new Thread(new Consumer(p)).start();
}
}
//定义一个产品类
class Products
{
private ReentrantLock lock = new ReentrantLock();
Condition condition_pro = lock.newCondition();
Condition condition_con = lock.newCondition();
private String name;
private int count;
private boolean flag;
public void input(String name)
{
lock.lock();
try
{
while(flag)
{
try
{
condition_pro.await();
}
catch(InterruptedException e)
{
System.out.println("线程唤醒异常!");
}
}
this.name = name+"---:"+count++;
System.out.println(Thread.currentThread().getName()+"..."+this.name);
flag=true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public void output()
{
lock.lock();
try
{
while(!flag)
try
{
condition_con.await();
}
catch(InterruptedException e)
{
System.out.println("线程唤醒异常!");
}
System.out.println(Thread.currentThread().getName()+"!!!!!!"+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
//定义两个线程类
class Producer implements Runnable
{
private Products p;
Producer(Products p)
{
this.p = p;
}
public void run()
{
while(true)
p.input("产品");
}
}
class Consumer implements Runnable
{
private Products p;
Consumer(Products p)
{
this.p = p;
}
public void run()
{
while(true)
p.output();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2