Thread.activeCount() in Java

Thread.activeCount(): This method is available in the java.lang.Thread class of Java.

Syntax:

int java.lang.Thread.activeCount()

This method returns an estimate of the number of active threads in the current thread's thread group and its subgroups. Recursively iterates over all subgroups in the current thread's thread group.

Parameters: NA

Returns: an estimate of the number of active threads in the current thread's thread group and any other thread group that has the current thread's thread group as an ancestor.

Exceptions: NA

Approach

Java

package com.Thread;

public class ThreadactiveCount {
    public static void main(String[] args) {

        System.out.println(Thread.activeCount());
    }
}

Output:

1


Some other methods of Thread

Thread()This method allocates a new Thread object.

Thread(Runnable)This method allocates a new Thread object with one argument.

Thread(String)This method allocates a new Thread object with the specified name.

Thread(Runnable, String)This method allocates a new Thread object with the specified runnable and the specified name.

Thread(ThreadGroup, Runnable)This method allocates a new Thread object with the specified ThreadGroup and the specified runnable.

Thread(ThreadGroup, String)This method allocates a new Thread object with the specified ThreadGroup and the specified name.

Thread(ThreadGroup, Runnable, String)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, and belongs to the thread group referred to by the group.

Thread(ThreadGroup, Runnable, String, long)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, belongs to the thread group referred to by the group, and has the specified stack size.

Thread(ThreadGroup, Runnable, String, long, boolean)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name belongs to the thread group referred to by group, has the specified stackSize, and inherits initial values for inheritable thread-local variables if inheritThreadLocals is true.

Thread.activeCount()This method returns an estimate of the number of active threads in the current thread's thread group and its subgroups. 

checkAccess()This method determines if the currently running thread has permission to modify this thread.

Constants Thread Class: Some of the constants of the Thread class, are Thread.MIN_PRIORITY, Thread.NORM_PRIORITY and Thread.MAX_PRIORITY.

Thread.currentThread()This method returns a reference to the currently executing thread object.

Thread.dumpStack()This method prints a stack trace of the current thread to the standard error stream.

Thread.enumerate(Thread[])This method copies into the specified array every active thread in the current thread's thread group and its subgroups.

Thread.getAllStackTraces()This method returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread.

getContextClassLoader()This method returns the context ClassLoader for this thread.

Thread.getDefaultUncaughtExceptionHandler()This method returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. If the returned value is null, there is no default.

getId()This method returns the identifier of this Thread. 

getName()This method returns this thread's name.

getPriority()This method returns this thread's priority.

getStackTrace()This method returns an array of stack trace elements representing the stack dump of this thread.

getState()This method returns the state of this thread.

getThreadGroup()This method returns the thread group to which this thread belongs.

No comments:

Post a Comment