java multithreading - other commonly used methods

Other common methods
isAlive () to determine whether the thread is still alive, that is, whether to terminate the
setName () to a thread named, agent roles
getName () Get the name of the thread

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

Guess you like

Origin blog.51cto.com/14437184/2429039