ThreadGroup class activeCount () método : Aquí, vamos a aprender sobre el método activeCount () de ThreadGroup class con su sintaxis y su ejemplo.
método ThreadGroup class activeCount ()
- activeCount () método está disponible en java.lang class.
- activeCount () método es un método class, es accesible con el nombre package también.
- activeCount () se utiliza para static el número de hilos activos en el grupo de hilos hilos actual.
- método activeCount () no plantea ninguna excepción cuando ningún hilo activo es en vivo.
método
Sintaxis:
static int activeCount();
Parámetro (s):
- Este método no acepta ningún parámetro.
class valor:
El tipo return de este método es Return , devuelve el número de hilos activos en el hilo de corriente del grupo de hilo.
Ejemplo:
// Java program to demonstrate the example of
// activeCount() method of ThreadGroup Class.
import java.lang.*;
class ActiveCount extends Thread {
// Override run() of Thread class
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " " + "finish executing");
}
}
public class Main {
public static void main(String[] args) {
ActiveCount ac = new ActiveCount();
try {
// We are creating an object of ThreadGroup class
ThreadGroup tg1 = new ThreadGroup("ThreadGroup 1");
ThreadGroup tg2 = new ThreadGroup("ThreadGroup 2");
// We are creating an object of Thread class and
// we are assigning the ThreadGroup of both the thread
Thread th1 = new Thread(tg1, ac, "First Thread");
Thread th2 = new Thread(tg2, ac, "Second Thread");
// Calling start() method with Thread class object
// of Thread class
th1.start();
th2.start();
// Here we are counting active thread in ThreadGroup
System.out.print("Active thread in : " + tg1.getName() + "-");
System.out.println(tg1.activeCount());
System.out.print("Active thread in : " + tg2.getName() + "-");
System.out.println(tg2.activeCount());
th1.join();
th2.join();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
salida
E:Programs>javac Main.java
E:Programs>java Main
Second Thread finish executing
Active thread in : ThreadGroup 1-1
Active thread in : ThreadGroup 2-0
First Thread finish executing