Home > Java > javaTutorial > Java Example - Get all threads

Java Example - Get all threads

黄舟
Release: 2017-01-20 11:24:55
Original
1659 people have browsed it

The following example demonstrates how to use the getName() method to obtain all running threads:

/*
 author by w3cschool.cc
 Main.java
 */public class Main extends Thread {
   public static void main(String[] args) {
      Main t1 = new Main();
      t1.setName("thread1");
      t1.start();
      ThreadGroup currentGroup = 
      Thread.currentThread().getThreadGroup();
      int noThreads = currentGroup.activeCount();
      Thread[] lstThreads = new Thread[noThreads];
      currentGroup.enumerate(lstThreads);
      for (int i = 0; i < noThreads; i++)
      System.out.println("线程号:" + i + " = " + lstThreads[i].getName());
   }}
Copy after login

The output of the above code is:

线程号:0 = main线程号:1 = thread1
Copy after login

The above is the Java example - Get For the content of all threads, please pay attention to the PHP Chinese website (www.php.cn) for more related content!




Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template