第一种:用Thread类创建线程

public class ThreadDemo1

{

public static void main(String args[])

{

new TestThread().start();//调TestThread类的start函数(从Thread类继承而来的)

while(true)

{

System.out.println("main thread is running");

}

}

}


class TestThread extends Thread

{

public void run()

{

while(true)

{

System.out.println(Thread.currentThread().getName() "is running");

}

}
}


第二种:使用Runnable接口创建多线程

public class ThreadDemo2

{

public static void main(String args[])

{

TestThread tt = new TestThread();//创建TestThread类的一个实例

Thread t = new Thread(tt);//创建一个Thread类的实例

t.start();//使线程进入Runnable状态

while(true)

{

System.out.println("main thread is running");

}

}

}


class TestThread implements Tunnable

{

public void run()//线程的代码段,当执行start()时,线程从此处开始执行

{

while(true)

{

System.out.println(Thread.currentThread().getName()"is running");

}

}

}


结论:第二种方法比较好。