深入研究Thread的currentThread()

来源:互联网 发布:淘宝一键装修免费模板 编辑:程序博客网 时间:2024/06/07 05:08

/** * 获取执行当前代码片段的线程 * Thread currentThread() * 可以调用thread提供的静态方法 * @author huawangxin * */public class ThreadDemo {public static void main(String[] args) {//获取的就是调用main方法的线程Thread t=Thread.currentThread();System.out.println("调用main的线程为:"+t);testCurrent();//创建一个线程Thread t1=new Thread(){public void run(){//获取当前线程Thread myt=Thread.currentThread();System.out.println("自己创建的线程为:"+myt);testCurrent();}};t1.start();}public static void testCurrent(){//获取调用这段方法的线程名称!Thread t=Thread.currentThread();System.out.println("调用testCurrent的方法的线程:"+t);}}



上述如果有不对或者补充的地方,请大家批评和指教,谢谢。

0 0