问题 问答题

下面的程序的功能是利用实现Runnable接口的方法来创建线程,并利用它来执行响应的一些操作。最后使得m的执行结果:100。


注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
class ClassName implements Runnable
int n;
_______________________
try
Thread. sleep (2000);
n=100;
catch(Exception e)

public static void main(String[] args)
try
ClassName a=new ClassName();
__________________
threadl.start();
threadl.join();
int m=a.n;
System.out.println("m="+m);
catch(Exception e)

答案

参考答案:
public void run()
Thread thread1=new Thread(a);

解析:
本题主要考查Java中对线程的创建知识。解答本题的关键是熟练掌握如何创建线程的知识。一般情况下,创建线程的方法是:(1)通过继承Thread类创建线程。(2)通过向Thread()构造方法传递Runnable对象来创建线程。在本题中,public void run()声明语句是用来声明线程体的,这是创建一个线程的必须做的。
Thread thread1=new Thread(a);语句的功能是通过向Thread()构造方法传递Runnable对象a来生成一个对象thread1。

单项选择题
多项选择题