📜  如何在Java中显示所有正在运行的线程?

📅  最后修改于: 2022-05-13 01:55:01.503000             🧑  作者: Mango

如何在Java中显示所有正在运行的线程?

一个线程 基本上是一个顺序执行的指令流。它用于在程序中实现多任务处理。一个程序可以有多个线程。线程用于同时执行多项操作。线程基本上是用来在后台执行复杂的任务而不影响主程序的。

Java中有两种方法可以显示所有正在运行的线程

1. 使用 ThreadGroup 对象

Java为我们提供了一种将多个线程分组到单个对象中的方法。在Java中,一组线程,即线程组是由 ThreadGroup 类实现的,所以这里我们将使用一个 ThreadGroup 对象来对当前运行的所有线程进行分组。在此之后,我们将使用 ThreadGroup 的 activeCount() 方法来获取活动线程的数量,然后我们将使用 ThreadGroup 的 enumerate(Thread[] list) 方法将当前活动的线程复制到一个数组中,并且我们将遍历数组以获取所有活动线程的名称。

Java
// Java Program to display all the running threads using
// ThreadGroup object
 
import java.io.*;
 
class GFGThread extends Thread {
    // overriden run method
    public void run()
    {
        System.out.println("Overriden Run Method");
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        // making a thread object
        GFGThread t1 = new GFGThread();
        GFGThread t2 = new GFGThread();
        t1.start(); // starting the thread
        t2.start();
 
        // getting the group of the threads/
        ThreadGroup threadGroup
            = Thread.currentThread().getThreadGroup();
 
        // getting the total active count of the threads
        int threadCount = threadGroup.activeCount();
 
        Thread threadList[] = new Thread[threadCount];
        // enumerating over the thread list
        threadGroup.enumerate(threadList);
 
        System.out.println("Active threads are:");
       
        // iterating over the for loop to get the names of
        // all the active threads.
        for (int i = 0; i < threadCount; i++)
            System.out.println(threadList[i].getName());
    }
}


Java
// Java Program to display all the running threads using
// getAllStackTraces() Method
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFGThread extends Thread {
   
    // overriden run method
    public void run()
    {
        System.out.println("Overriden Run Method");
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        // making a thread object
        GFGThread t1 = new GFGThread();
        GFGThread t2 = new GFGThread();
        t1.start(); // starting the thread
        t2.start();
 
        // getAllStackTraces() method will return the Set of
        // Thread Objects
        Set threadSet
            = Thread.getAllStackTraces().keySet();
        // iterating over the threads to get the names of
        // all the active threads
        for (Thread x : threadSet) {
            System.out.println(x.getName());
        }
    }
}


2. 使用 getAllStackTrace() 方法

getAllStackTrace() 方法提供所有正在运行的线程的堆栈跟踪。然后我们创建该元素的一组键,因为该方法返回一个映射,然后我们循环遍历该集合的所有元素以打印所有正在运行的线程。

Java

// Java Program to display all the running threads using
// getAllStackTraces() Method
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFGThread extends Thread {
   
    // overriden run method
    public void run()
    {
        System.out.println("Overriden Run Method");
    }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        // making a thread object
        GFGThread t1 = new GFGThread();
        GFGThread t2 = new GFGThread();
        t1.start(); // starting the thread
        t2.start();
 
        // getAllStackTraces() method will return the Set of
        // Thread Objects
        Set threadSet
            = Thread.getAllStackTraces().keySet();
        // iterating over the threads to get the names of
        // all the active threads
        for (Thread x : threadSet) {
            System.out.println(x.getName());
        }
    }
}

输出

Overriden Run Method
Overriden Run Method
Thread-0
Reference Handler
Thread-1
Signal Dispatcher
main
Finalizer