java 多线程-常用其他方法

常用其他方法
isAlive() 判断线程是否还活着,即是否终止
setName() 给线程起名,代理角色
getName() 获取线程名字

public class n {

public static void main(String[]args) throws InterruptedException
{
    System.out.println(Thread.currentThread().isAlive());

    //设置名字,真实角色(面向对象方式设置)和代理角色(线程的默认名字0到10)
    test t=new test("ha");
    Thread tt=new Thread(t);
    tt.setName("ff"); //设置的是代理角色名称
    tt.start();
    Thread.sleep(1000);
    System.out.println(tt.isAlive());
}
}

class test implements Runnable{
private String name;
public test(String name)
{
    this.name=name;
}
public void run()
{
    System.out.println(Thread.currentThread().getName()+"-->"+name);
}
}

猜你喜欢

转载自blog.51cto.com/14437184/2429039