• 获取线程名称、设置线程名称、获取当前所有线程


    一、获取/设置线程名称

     1 public class ThreadTest {
     2 
     3     public static void main(String[] args) {
     4         setThreadName1();
     5         setThreadName2();
     6         setThreadName3();
     7         getThread();
     8     }
     9 
    10 }

    1、获取线程名字

    thread.getName()

    2、设置线程名字

    有两种设置线程名字的方法:a. 使用构造方法;b. 调用thread.setName()方法。

    注:线程名称默认为Thread-0、Thread-1

    1)通过构造函数可以传入String类型的名字

     1   /**
     2      * 方法一:通过构造函数可以传入String类型的名字
     3      */
     4     public static void setThreadName1() {
     5         new Thread("thread-name-1") {
     6             public void run() {
     7                 for(int i = 0; i < 3; i++) {
     8                     System.out.println("threadName1: " + this.getName());
     9                 }
    10             }
    11         }.start();
    12     }

    2)通过setName(String)方法可以设置线程的名字

     1  /**
     2      * 方法二:通过setName(String)方法可以设置线程对象的名字
     3      */
     4     public static void setThreadName2() {
     5         new Thread() {
     6             public void run() {
     7                 this.setName("thread-name-2");
     8                 for(int i = 0; i < 3; i++) {
     9                     System.out.println("threadName2: " + this.getName());
    10                 }
    11             }
    12         }.start();
    13     }
    14 
    15     public static void setThreadName3() {
    16         Thread thread = new Thread() {
    17             public void run() {
    18                 for(int i = 0; i < 3; i++) {
    19                     System.out.println("threadName3: " + this.getName());
    20                 }
    21             }
    22         };
    23 
    24         thread.setName("thread-name-3");
    25         thread.start();
    26     }

    3、获取当前线程的对象

    • currentThread():返回对当前正在执行的线程对象的引用,返回的是一个Thread
    • Thread.currentThread():也可以用于获取主线程
     1     public static void getThread() {
     2         new Thread(new Runnable() {
     3             public void run() {
     4                 for(int i = 0; i < 3; i++) {
     5                     System.out.println("当前线程名称:" + Thread.currentThread().getName());
     6                 }
     7             }
     8         }).start();
     9 
    10         new Thread(new Runnable() {
    11             public void run() {
    12                 for(int i = 0; i < 3; i++) {
    13                     System.out.println("当前线程名称:" + Thread.currentThread().getName());
    14                 }
    15             }
    16         }).start();
    17         Thread.currentThread().setName("我是主线程");    //获取主函数线程的引用,并改名字,
    18         System.out.println("当前线程名称:" + Thread.currentThread().getName());    //获取主函数线程的引用,并获取名字
    19     }

    二、获取当前所有的线程

     1     public void obtainALlThread() {
     2 
     3         ThreadGroup group = Thread.currentThread().getThreadGroup();
     4         ThreadGroup topGroup = group;
     5         // 遍历线程组树,获取根线程组
     6         while (group != null) {
     7             topGroup = group;
     8             group = group.getParent();
     9         }
    10         // 激活的线程数加倍
    11         int estimatedSize = topGroup.activeCount() * 2;
    12         Thread[] slackList = new Thread[estimatedSize];
    13         // 获取根线程组的所有线程
    14         int actualSize = topGroup.enumerate(slackList);
    15         // copy into a list that is the exact size
    16         Thread[] list = new Thread[actualSize];
    17         System.arraycopy(slackList, 0, list, 0, actualSize);
    18         System.out.println("Thread list size == " + list.length);
    19         for (Thread thread : list) {
    20             System.out.println(thread.getName());
    21         }
    22     }

    三、参考

    https://blog.csdn.net/love_Aym/article/details/79953221

    https://blog.csdn.net/Deronn/article/details/80247484

  • 相关阅读:
    被隐藏的文件更改为可见
    Selenium WebDriver多层表单切换
    for循环
    Java课程设计二次大作业
    Java-DAO模式代码阅读及应用
    编辑器、编译器、文件、IDE等常见概念辨析
    树、二叉树和查找等知识点的总结
    二叉树的实现
    二叉树顺序结构和链式结构的相互转换
    使用k-近邻算法改进约会网站的配对效果
  • 原文地址:https://www.cnblogs.com/zeroingToOne/p/11440905.html
Copyright © 2020-2023  润新知