线程的常用方法:
public class TestThread { public static void main(String[] argc) throws Exception { Runnable r = new MyThread(); Thread t = new Thread(r, "Name test");//定义线程对象,并传入参数; t.start();//启动线程; System.out.println("name is: " + t.getName());//输出线程名称; Thread.currentThread().sleep(5000);//线程暂停5分钟; System.out.println(t.isAlive());//判断线程还在运行吗? System.out.println("over!"); } } class MyThread implements Runnable { //线程体; public void run() { for (int i = 0; i < 10; i++) System.out.println(i); } }
执行结果如图所示: