设置和获取 线程名称

设置和获取 线程名称

  1. currentThread
  2. getName
  3. setName
package xc;

public class Demo3 {
    
    
    /**
     * currentThread:获取当前正在执行的线程对象
     * getName:获取当前正在执行的线程名称
     * setName:修改名称
     */
    public static void main(String[] args) {
    
    
        System.out.println(Thread.currentThread().getName());

        MyRunnable m = new MyRunnable();
        Thread t = new Thread(m);
        t.setName("hhh");
        t.start();

        new Thread(new MyRunnable()).start();
        new Thread(new MyRunnable()).start();
    }
    static class MyRunnable implements Runnable{
    
    

        @Override
        public void run() {
    
    
            System.out.println(Thread.currentThread().getName());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_56187876/article/details/115001076
今日推荐