下列程序的功能是在监控台上每隔一秒钟显示一个字符串"Hello!",能够填写在程序中下划线位置,使程序完整并能正确运行的语句是( )。 public class Test implements Runnable{ public static void main (String args[]){Test t=new Test ();Thread tt=new Thread(t);tt.start(); } public void run(){for(;;) { try{ ______; }catch( ______ e) {} System.out.println("Hello");} } }
A.sleep(1000)InterruptedException
B.sleep(1000)InterruptedException
C.Threasleep(1000)RuntimeException
D.Threasleep(1000)InterruptedException
参考答案:D
解析: 本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程, Test t=new Test()语句定义了Test的1个实例, Thread tt=new Thread(t)定义了1个名为tt的线程,tt.start()语句启动线程。通过try-catch语句来处理异常。try代码包括一些简单语句或方法调用,遇到异常情况时,停止执行而跳转到相应处理异常的程序,然后由catch来控制。题目要求每间隔1s输出,间隔使用Thread.sleep (1000)语句来实现,调用InterruptedException来完成。RuntimeException类包含有较多子类,比如算术异常ArithmeticException,当除法分母为0等时使用;索引越界异常IndexOutOf BoundsException等。