黑马程序员技术交流社区
标题:
condition中await方法的异常应该怎么处理?
[打印本页]
作者:
周素强
时间:
2012-5-5 11:07
标题:
condition中await方法的异常应该怎么处理?
本帖最后由 周素强 于 2012-5-5 11:10 编辑
import java.util.concurrent.locks.*;
class ProducerConsumerLock
{
public static void main(String[] args)
{
Resource r = new Resource();
Thread pro1 = new Thread(new Producer(r));
Thread pro2 = new Thread(new Producer(r));
Thread con1 = new Thread(new Consumer(r));
Thread con2 = new Thread(new Consumer(r));
pro1.start();
pro2.start();
con1.start();
con2.start();
}
}
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
Lock lock = new ReentrantLock();
Condition con_pro = lock.newCondition();
Condition con_con = lock.newCondition();
public void set(String name)
{
lock.lock();
try
{
while(flag)
try
{
con_pro.await();
}
catch (InterruptedException e)
{
//此处的异常应该怎么处理,我的意思是说线程真有冻结时应该怎么处理,Thread的interrupt()方法应该用在哪里?
}
this.name = name+"---"+count++;
System.out.println(Thread.currentThread().getName()+".....生产者"+this.name);
flag = true;
con_con.signal();
}
finally
{
lock.unlock();
}
}
public void out()
{
lock.lock();
try
{
while(!flag)
try
{
con_con.await();
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"..消费者"+this.name);
flag = false;
con_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();
}
}
}
复制代码
作者:
韩慧阳
时间:
2012-5-5 11:31
interrupt方法在执行后本身就会抛出一个异常,所以不应该把interrupt写在catch里面,这样一定会报错~~~加分哈~~~{:soso_e144:}
作者:
永恒之翼网络
时间:
2012-5-5 14:43
public void set(String name)
30. {
31. lock.lock();
32. try
33. {
34. while(flag)
35. try
36. {
37. con_pro.await();
38. }
39. catch (InterruptedException e)
40. {
41. e.printStackTrace(); //
此处打印异常信息就可以了
,
42. }
43.
44. this.name = name+"---"+count++;
45. System.out.println(Thread.currentThread().getName()+".....生产者"+this.name);
46. flag = true;
47. con_con.signal();
48. }
49. finally
50. {
51. lock.unlock();
52. }
53.
54. }
在main()方法中添加interrupt()方:
public static void main(String[] args)
05. {
06. Resource r = new Resource();
07.
08. Thread pro1 = new Thread(new Producer(r));
09. Thread pro2 = new Thread(new Producer(r));
10. Thread con1 = new Thread(new Consumer(r));
11. Thread con2 = new Thread(new Consumer(r));
12. pro1.start();
13. pro2.start();
14. con1.start();
15. con2.start();
con1.interrupt();
con2.interrup();
pro1.interrup();
16. }
四个线程都可以调用interrupt方法,随意。out()方法里的异常可以像set()方法里一样处理。如果想异常信息更详细,可以在catch里添加一条打印中文的字符串,说明是什么样的异常。
作者:
永恒之翼网络
时间:
2012-5-5 14:43
public void set(String name)
30. {
31. lock.lock();
32. try
33. {
34. while(flag)
35. try
36. {
37. con_pro.await();
38. }
39. catch (InterruptedException e)
40. {
41. e.printStackTrace(); //
此处打印异常信息就可以了
,
42. }
43.
44. this.name = name+"---"+count++;
45. System.out.println(Thread.currentThread().getName()+".....生产者"+this.name);
46. flag = true;
47. con_con.signal();
48. }
49. finally
50. {
51. lock.unlock();
52. }
53.
54. }
在main()方法中添加interrupt()方:
public static void main(String[] args)
05. {
06. Resource r = new Resource();
07.
08. Thread pro1 = new Thread(new Producer(r));
09. Thread pro2 = new Thread(new Producer(r));
10. Thread con1 = new Thread(new Consumer(r));
11. Thread con2 = new Thread(new Consumer(r));
12. pro1.start();
13. pro2.start();
14. con1.start();
15. con2.start();
con1.interrupt();
con2.interrup();
pro1.interrup();
16. }
四个线程都可以调用interrupt方法,随意。out()方法里的异常可以像set()方法里一样处理。如果想异常信息更详细,可以在catch里添加一条打印中文的字符串,说明是什么样的异常。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2