5.1 在(1)~(3)处填上适当的语句,使程序能正常运行。 class MyThread implements Runable { (1) { while(true){System.out.print("hello"); try { (2) //休眠1秒钟 } catch (InterruptedException e) { e.printStackTrace(); } }}} public class Demo{ public static void main(String []s){ MyThread thread1= (3) //声明创建对象thread1 Thread thread = new Thread(thread1,"线程1") thread.start(); //通过对象thread启动线程 }}
举一反三
- class X implements Runnable{public static void main(String args[]){/* Missing code? */}public void run() {}}哪一行代码适合启动线程? A: Thread t = new Thread(X); B: Thread t = new Thread(X);t.start(); C: X run = new X();Thread t = new Thread(run);t.start(); D: Thread t = new Thread();x.run();
- 下列程序创建了一个线程并运行,请在下画线处填入正确代码。 public class Try extends Thread public static void main(String args[]) Thread t=new Try(); ______; public void run() System.out.printin("Try!");
- 下列程序的执行结果是______。 class A1 implements Runnable { public void run() { for(iht i = 0; i < 10; i++) { System.out.println("i =" + i); if(i == 3) break; } } } public class ex38 { public static void main(String[] args) { Thread th1 = new Thread(new A1()); th1.start (); } } A: i=1 i=2 i=3 i=4 B: i=1 i=1 i=1 i=1 C: i=0 i=1 i=2 i=3 D: i=1 i=2 i=3
- Given: 1. public class Foo implements Runnable { 2. public void run (Thread t) { 3. System.out.println("Running."); 4. } 5. public static void main (String[] args) { 6. new Thread (new Foo()).start(); 7. } 8. } What is the result?
- 下列程序的功能是在监控台上每一秒种显示一个字符串“Hello!”,能够填写在线程中下划线位置,使程序完整并能正确运行的语句是 public class Test implements Runnable{ public static void main(String args[]){ Test t=new Test(); Thread tt=new Thread(t); tt.start(); }