单选题
请阅读下面程序,说明该程序创建线程使用的方法是( )。
public class ThreadTest
{
public static void main(String args[])
{
Thread tl=new Thread(new HolloWorld());
Thread t2=new Thread(new HolloWorld());
tl.start();
t2.Start();
}
}
class HolloWorld implements Runnable
{
int i;
public void run()
{
while(true)
{
System.out.println("HolloWorld"+i++);
if(i= =5)break;
}
}
}
【正确答案】
B
【答案解析】解析:本题考查线程的创建。在Java中,创建线程有两种方法:①通过实现Runnable接口创建线程。Runnable接口中只定义了一个run()方法作为线程体。②通过继承Thread类创建线程,Thread类本身实现了Runnable接口。创建的新的线程不会自动运行,必须调用start()方法才能运行。本题中HolloWorld类实现了Runnable接口。