adsense


Sunday, 1 November 2015

JAVA EXAMPLES DISPLAY ALL RUNNING THREAD PAGE 195

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES DISPLAY ALL RUNNING THREAD








Java Examples - Display all running Thread?



Problem Description:
How to display all running Thread?
Solution:
Following example demonstrates how to display names of all the running threads using getName() method.
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("Thread No:" + i + " = "
      + lstThreads[i].getName());
   }
}
Result:
The above code sample will produce the following result.
Thread No:0 = main
Thread No:1 = thread1


No comments: