public class Java_2 { public static void main (String args[]) { try { Sleep a = new Sleep (); Thread t = new Thread (a); //*********Found********** t._____________(); t.join(); int j= a.i; System.out.println("j="+j+",a.i="+a.i); } catch (Exception e) {} } } //*********Found********** class Sleep implements _________________{ int i; public void run () { try { //*********Found********** Thread._______________(50); i= 10; } catch(InterruptedException e) {} } }
时间: 2023-07-18 17:46:17 浏览: 110
Java sleep方法及中断方式、yield方法代码实例
以下是完整的Java程序,填空的地方已经标注出来了:
```
public class Java_2 {
public static void main(String args[]) {
try {
Sleep a = new Sleep();
Thread t = new Thread(a);
t.start(); // 填空1
t.join();
int j = a.i;
System.out.println("j=" + j + ",a.i=" + a.i);
} catch (Exception e) {
}
}
}
class Sleep implements Runnable {
int i;
public void run() {
try {
Thread.sleep(50); // 填空2
i = 10;
} catch (InterruptedException e) {
}
}
}
```
这个程序创建了一个Sleep对象,并将其传递给一个新线程的构造函数。新线程开始执行后,它将暂停50毫秒,然后将Sleep对象的i字段设置为10。主线程等待新线程完成后,将Sleep对象的i字段的值打印到控制台上。程序的输出应该是:
```
j=10,a.i=10
```
阅读全文