java——获取线程名称

2019年2月17日20:55:35 发表评论 1,244 views
/**
 * Created by xabcd on 2019/2/17.
 */
class GetNameThread extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++)
            printMsg();
//

    }

    public void printMsg()
    {
        Thread ge = Thread.currentThread();
        String name = ge.getName();
        System.out.println("name = "+name);
    }

    public static void main(String[] args)
    {
        GetNameThread ge = new GetNameThread();
        ge.start();
        for(int i = 0;i<10;i++)
        {
            ge.printMsg();
        }
    }
}

结果:
name = main
name = main
name = main
name = main
name = main
name = main
name = main
name = main
name = main
name = main
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0
name = Thread-0

发表评论

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