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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马李文 中级黑马   /  2013-5-29 09:52  /  1610 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黑马李文 于 2013-5-30 08:50 编辑

代码运行结果是什么??
  1. import java.io.*;

  2. public class MyThread extends Thread{
  3.         public static void main(String args[]){
  4.                 MyThread mt = new MyThread();
  5.                 mt.start();
  6.         }

  7.         public void run(){
  8.                 try {
  9.                              sleep(200);
  10.                              System.out.println
  11. ("Printing from thread run() method");
  12.                 } catch ( IOException e) {
  13.                         
  14.                 }
  15.         }
  16. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

5 个回复

倒序浏览
应该是输出"Printing from thread run() method",可是不会抛出IOException异常吧,是不是该报"IO"去掉。
回复 使用道具 举报
这段代码编译期是不会通过的,首先不会抛出IOException,会提示去掉这个异常,其次用到sleep方法时,需要捕获InterruptedException异常,要不然就抛出异常。
回复 使用道具 举报
public class MyThread extends Thread{
        public static void main(String args[]){
                MyThread mt = new MyThread();
                mt.start();
        }

        public void run(){
                try {
                             sleep(200);
                             System.out.println
("Printing from thread run() method");
                } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
        }
}
帮你修改了下,在catch这里有误
   



                        

                }

        }

}


评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

回复 使用道具 举报
同楼上
编译同不过的
用到sleep方法,需要处理InterruptedException异常;
不会抛出IOException,你却catch了




把catch ( IOException e) 里的io异常换成  InterruptedException e 就可以了

输出就是:Printing from thread run() method
回复 使用道具 举报
如果问题已解决,请及时修改分类,否则继续提问,谢谢合作!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马