下面的程序定义了一个线程类,请填空把程序补充完整。 public class MyThread implements (1) { private int num = 0; private boolean stop = false; public static void main(String[] args) { MyThread my = new MyThread(); Thread td = new Thread(my); td.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } my.setStop(true); } // 循环显示默认线程名和当前的num值后睡眠200毫秒,然后继续,直到变量stop为true为止。 public void (2) { while (!stop) { // 返回当前线程的名称 String threadName = (3) ; System.out.println(threadName + ":" + num); num++; try { // 睡眠200毫秒 (4) ; } catch (InterruptedException e) { e.printStackTrace(); } } } // 设置成员变量的值。 public void setStop(boolean stop) { this.stop = stop; } } 填空答案: (1) (2) (3) (4) 运行截图:
时间: 2024-02-14 16:10:14 浏览: 67
填空答案:
(1) Runnable
(2) run
(3) Thread.currentThread().getName()
(4) Thread.sleep(200)
运行截图:
```
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
```
相关问题
1.下面的程序定义了一个线程类,请填空把程序补充完整。 public class MyThread implements (1) { private int num = 0; private boolean stop = false; public static void main(String[] args
) {
MyThread thread = new MyThread();
thread.start();
}
public void run() {
while(!stop) {
num++;
System.out.println("Current number is: " + num);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public void stopRunning() {
stop = true;
}
}
Runnable
.下面的程序定义了一个线程类,请填空把程序补充完整。 public class MyThread implements (1) { private int num = 0; private boolean stop = false; public static void main(String[] args) { MyThread my = new MyThread(); Thread td = new Thread(my);
public class MyThread implements Runnable {
private int num = 0;
private boolean stop = false;
public void run() {
while(!stop) {
System.out.println("num = " + num);
num++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread my = new MyThread();
Thread td = new Thread(my);
td.start();
}
}
阅读全文