[问答题] 下面的程序的功能是利用实现Runnable接口的方法来创建线程,并利用它来执行响应的一些操作。最后使得m的执行结果为100。 注意:请勿改动mai
2021-07-20
[问答题] 下面的程序的功能是利用实现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();
_____
thread1.start();
thread1.join();
int m=a.n;
System.out.println("m="+m);
catch(Exception e)
正确答案:public void run()Thread thread1=new Thread(a);①通过继承Thread类创建线程:②通过向Thread()构造方法传递Runnable对象来创建线程。在本题中,public void run()声明语句是用来声明线程体的,这是创建一个线程的必须做的。Thread thread1=new Thread(a)语句的功能是通过向Thread()构造方法传递Runnable对象a来生成一个对象thread1。最后程序的输出结果如下图所示。
参考解析:本题主要考查Java中对线程的创建知识。解答本题的关键是熟练掌握如何创建线程的知识。一般情况下,创建线程的方法是:
词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。
