A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© nestor 中级黑马   /  2014-4-2 13:34  /  889 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 nestor 于 2014-4-2 14:54 编辑


  1. class Res{
  2.         private String name;
  3.         private String sex;
  4.         private boolean flag = false;
  5.         
  6.         public synchronized void set(String name,String sex){
  7.                 if(flag)
  8.                         try{this.wait();}catch(Exception e){}
  9.                 this.name = name;
  10.                 this.sex = sex;
  11.                 flag = true;
  12.                 this.notify();
  13.         }
  14.         public synchronized void out(){
  15.                 if(!flag)
  16.                         try{this.wait();}catch(Exception e){}
  17.                 System.out.println(name+"##"+sex);
  18.                 flag = false;
  19.                 this.notify();
  20.         }
  21. }

  22. class Input implements Runnable{
  23.         private Res r;
  24.         Input(Res r){
  25.                 this.r = r;
  26.         }
  27.         public void run(){
  28.                 int x = 0;
  29.                 while(true){
  30.                         if(x==0)
  31.                                 r.set("li", "man");
  32.                         else
  33.                                 r.set("wang", "woman");
  34.                         x = (x+1)%2;
  35.                         
  36.                 }
  37.         }
  38. }

  39. class Output implements Runnable{
  40.         private Res r;
  41.         Output(Res r){
  42.                 this.r = r;
  43.         }
  44.         public void run() {
  45.                 while(true){
  46.                         r.out();                        
  47.                 }

  48.         }
  49. }
  50. class InputOut{
  51.         public static void main(String[] args){
  52.                 Res r = new Res();
  53.                 new Thread(new Input(r)).start();
  54.                 new Thread(new Output(r)).start();
  55.         }
  56. }
复制代码



if(flag) try{this.wait();}catch(Exception e){}
  
为什么第8行和第16行需要处理异常?

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

5 个回复

倒序浏览
这个方法是抛出异常的 ,所以你在调用一个抛出异常的方法时 要么try catch  要么调用的函数上继续抛出异常
public final void wait()  throws InterruptedException

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
这是要抛异常的,这个你如果用IDE工具开发,一般都会有提示你的,要你就到JDK文档上看看,这个方法要不要抛异常,需要抛异常就按它的要求做处理,至于两哪种方式,楼上的已经讲了,我就不再细说了。
回复 使用道具 举报
本帖最后由 Peach2014 于 2014-4-2 15:52 编辑

public final void wait() throws InterruptedException
这个是wait()方法的概要,因为wait()方法抛出异常,在使用的就需要处理这个异常,对于异常的处理有两种方法:1,继续抛出;2,直接处理(try-catch)
这里使用的是第二种方法。
回复 使用道具 举报
基本上所有的流都是带有异常的,因为可能出现的错太多了。所有源码上面本身就要求要处理这个异常。
回复 使用道具 举报
  1. public
  2. interface Runnable {
  3.     public abstract void run();
  4. }

  5. 以上是Runnable的源代码
  6. 实现Runnable接口的时候,必须要覆盖run()方法。
  7. 如果不在run()里面用try catch处理异常,则只能写成如下。
  8. 而如下代码不是Runnable接口中run()方法的重写,编译都没法通过的
  9. public void run() throws Exception
  10. {

  11. };
复制代码

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马