Home Tutorial Java Core Count Active Thread in JAVA

 
 

Share on Google+Share on Google+
Count Active Thread in JAVA
Posted on: November 8, 2012 at 12:00 AM
Advertisement
In this tutorial, we are using activeCount() method of thread to count the current active threads.

Count Active Thread in JAVA

In this tutorial, we are using activeCount() method of thread to count the current active threads.

Thread activeCount() :

Thread class provides you to check the current active thread by providing activeCount() method.

activeCount() :  This method returns the total number of active threads in the current thread group.

Example :

class ThreadCount implements Runnable {
	Thread th;
	String str;

	public ThreadCount(String str) {
		this.str = str;
		th = new Thread(this);
		th.start();
	}

	public void run() {
		try {
			Thread.sleep(500);

		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	}
}

class ThreadActiveCount {

	public static void main(String args[]) {
		new ThreadCount("A");
		new ThreadCount("B");
		new ThreadCount("C");
		/* Thread.activeCount() returns active threads */
		System.out
				.println("Number of active threads : " + Thread.activeCount());
	}
}

Output :

Number of active threads : 4
Advertisement

Related Tags for Count Active Thread in JAVA :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 8, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com