方式一:
public class ThreadTest
{
public static void main(String[] args) throws Exception
{
new MyThread().start();
}
}
class MyThread extends Thread
{
public void run()
{
while(true)
{
System.out.println("Thread say:Hello,World!");
try
{
sleep(5000);
}
catch (InterruptedException e)
{
}
}
}
}
方式二:
public class ThreadTest2
{
public static void main(String[] args)
{
Thread t1=new Thread(new MyThreadRun());
Thread t2=new Thread(new MyThreadRun());
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThreadRun implements Runnable
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
}