?java——判断线程是否启动

2019年2月17日21:47:39 发表评论 1,255 views
/**
 * Created by xabcd on 2019/2/17.
 */
public class StratThread extends Thread
{
    public void run()
    {
        for (int i = 0;i<10;i++);{
        PrintMsg();
    }
    }
    public void PrintMsg(){
        Thread t = Thread.currentThread();
        String name = t.getName();
        System.out.println("name = "+name);
    }

    public static void main(String args[])
{
    StratThread tt = new StratThread();
    tt.setName("test Thread");
    System.out.println("启动前:"+tt.isAlive());
    tt.start();
    System.out.println("刚启动:"+tt.isAlive());
    for (int i = 0;i<3;i++)
    {
        tt.PrintMsg();
    }
    //下面语句结果是不固定的,又是输出true有时是false
    System.out.println("main()方法结束时:"+tt.isAlive());
}
}





启动前:false
刚启动:true
name = main
name = main
name = main
main()方法结束时:true
name = test Thread





结果总是只有一个test Thread

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: