黑马程序员技术交流社区
标题:
看了半天,都看晕了,还没找到我的问题在哪?求检查代码
[打印本页]
作者:
欧德林
时间:
2012-4-8 14:21
标题:
看了半天,都看晕了,还没找到我的问题在哪?求检查代码
import java.util.concurrent.locks.*;
class ProducerConsumerDeme2
{
public static void main(String[] args)
{
Resource r =new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
Thread t1= new Thread (pro);
Thread t2 = new Thread (con);
Thread t3= new Thread (pro);
Thread t4 = new Thread (con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count =1;
private boolean flag= false;
private Lock lock= new ReentrantLock ();
private Condition condition_pro= lock.newCondition();
private Condition condition_con= lock.newCondition();
public void set(String name) throws InterruptedException
{
lock.lock();
try
{
while(flag)
condition_pro.await();
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag=true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();
System.out.println((Thread.currentThread()).getName()+"...消费者........."+this.name);
flag =false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer (Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
res.set("+商品+");
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer (Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
res.out();
}
}
}
提示 是 下面。我这不是写了 throws InterruptedException 怎么还有这个错误。头大。。。。
ProducerConsumerDeme2.java:86: 未报告的异常 java.lang.InterruptedException;必须
对其进行捕捉或声明以便抛出
res.set("+商品+");
^
1 错误
作者:
翟友伟
时间:
2012-4-8 14:31
public void set(String name)
res.set("+商品+");
是不是参数不对
this.name=name+"--"+
count++;
private
int count
=1;
作者:
邓海涛
时间:
2012-4-8 14:36
本帖最后由 邓海涛 于 2012-4-8 14:42 编辑
import java.util.concurrent.locks.*;
class ProducerConsumerDeme2
{
public static void main(String[] args)
{
Resource r =new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
Thread t1= new Thread (pro);
Thread t2 = new Thread (con);
Thread t3= new Thread (pro);
Thread t4 = new Thread (con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count =1;
private boolean flag= false;
private Lock lock= new ReentrantLock ();
private Condition condition_pro= lock.newCondition();
private Condition condition_con= lock.newCondition();
public void set(String name) throws InterruptedException
{
lock.lock();
try
{
while(flag)
condition_pro.await();
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag=true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();
System.out.println((Thread.currentThread()).getName()+"...消费者........."+this.name);
flag =false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer (Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
try{
res.set("+商品+");
catch(Exception e){
e.printStackTrace() ;
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer (Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
try{
res.out();
}catch(Exception e){
e.printStackTrace() ;
}
}
}
}
朋友这样就可以了,是因为你没有对异常进行捕捉,所以才错误,你比较下代码吧,我对异常进行捕捉以后,可以正常运行了
作者:
翟友伟
时间:
2012-4-8 14:36
不能抛 那只能 try catch了啊~
作者:
邓海涛
时间:
2012-4-8 14:39
import java.util.concurrent.locks.*;
class ProducerConsumerDeme2
{
public static void main(String[] args)
{
Resource r =new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
Thread t1= new Thread (pro);
Thread t2 = new Thread (con);
Thread t3= new Thread (pro);
Thread t4 = new Thread (con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count =1;
private boolean flag= false;
private Lock lock= new ReentrantLock ();
private Condition condition_pro= lock.newCondition();
private Condition condition_con= lock.newCondition();
public void set(String name) throws InterruptedException
{
lock.lock();
try
{
while(flag)
condition_pro.await();
this.name=name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag=true;
condition_con.signal();
}
finally
{
lock.unlock();
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();
System.out.println((Thread.currentThread()).getName()+"...消费者........."+this.name);
flag =false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer (Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
try {
res.set("+商品+");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer (Resource res)
{
this.res=res;
}
public void run()
{
while(true)
{
try {
res.out();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
朋友不好意思啊,刚才那个粘贴错了,改了的代码在这里,进行了异常捕捉。你看看吧
作者:
邓海涛
时间:
2012-4-8 14:45
还有点我想给你说的是,你的异常你只是在你写的方法里面抛出了异常,但是在main方法里面没有抛出异常,这样你肯定会错误啊,你试试同时在main方法那里也抛出异常,我敢保证一定能正确运行,上面那个是我捕捉了异常,第二种就是你在main方法里面也抛出异常,那就没问题了!去试试吧
作者:
邓海涛
时间:
2012-4-8 14:47
哥们我在给你补充点,你相当于是方法把异常抛出来了,但是main方法没有抛啊,就相当于,一个公司的小组长遇到不能解决的问题,他只有把问题抛给比他级别更高的领导解决啊,所以下次记住了如果方法抛了异常的话,main方法一定也要抛出异常,不然main方法也会处理不了的,main方法处理不了的,你抛出来让JVM去处理
作者:
欧德林
时间:
2012-4-8 15:58
邓海涛 发表于 2012-4-8 14:45
还有点我想给你说的是,你的异常你只是在你写的方法里面抛出了异常,但是在main方法里面没有抛出异常,这样 ...
谢谢哈,你比较牛!!
作者:
邓海涛
时间:
2012-4-8 16:17
呵呵。大家相互学习!
作者:
pray
时间:
2014-4-26 05:39
让人翻译成36种不同外语流传国内外世界各地的好帖
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2