Java获取线程基本信息的方法

表11-1线程的常用方法.png

线程的常用方法:

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);
    }
}

      执行结果如图所示:

 

posted @ 2020-02-17 13:40  赵广陆  阅读(64)  评论(0编辑  收藏  举报