Added in API level 1

ThreadGroup

open class ThreadGroup : Thread.UncaughtExceptionHandler
kotlin.Any
   ↳ java.lang.ThreadGroup

A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.

A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.

Summary

Public constructors

Constructs a new thread group.

ThreadGroup(parent: ThreadGroup!, name: String!)

Creates a new thread group.

Public methods
open Int

Returns an estimate of the number of active threads in this thread group and its subgroups.

open Int

Returns an estimate of the number of active groups in this thread group and its subgroups.

open Boolean

Used by VM to control lowmem implicit suspension.

Unit

Determines if the currently running thread has permission to modify this thread group.

Unit

Destroys this thread group and all of its subgroups.

open Int

Copies into the specified array every active thread in this thread group and its subgroups.

open Int
enumerate(list: Array<Thread!>!, recurse: Boolean)

Copies into the specified array every active thread in this thread group.

open Int

Copies into the specified array references to every active subgroup in this thread group and its subgroups.

open Int
enumerate(list: Array<ThreadGroup!>!, recurse: Boolean)

Copies into the specified array references to every active subgroup in this thread group.

Int

Returns the maximum priority of this thread group.

String!

Returns the name of this thread group.

ThreadGroup!

Returns the parent of this thread group.

Unit

Interrupts all threads in this thread group.

Boolean

Tests if this thread group is a daemon thread group.

open Boolean

Tests if this thread group has been destroyed.

open Unit

Prints information about this thread group to the standard output.

Boolean

Tests if this thread group is either the thread group argument or one of its ancestor thread groups.

Unit

Resumes all threads in this thread group.

Unit
setDaemon(daemon: Boolean)

Changes the daemon status of this thread group.

Unit

Sets the maximum priority of the group.

Unit

Stops all threads in this thread group.

Unit

Suspends all threads in this thread group.

open String

Returns a string representation of this Thread group.

open Unit

Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

Public constructors

ThreadGroup

Added in API level 1
ThreadGroup(name: String!)

Constructs a new thread group. The parent of this new group is the thread group of the currently running thread.

The checkAccess method of the parent thread group is called with no arguments; this may result in a security exception.

Parameters
name String!: the name of the new thread group.
Exceptions
java.lang.SecurityException if the current thread cannot create a thread in the specified thread group.

ThreadGroup

Added in API level 1
ThreadGroup(
    parent: ThreadGroup!,
    name: String!)

Creates a new thread group. The parent of this new group is the specified thread group.

The checkAccess method of the parent thread group is called with no arguments; this may result in a security exception.

Parameters
parent ThreadGroup!: the parent thread group.
name String!: the name of the new thread group.
Exceptions
java.lang.NullPointerException if the thread group argument is null.
java.lang.SecurityException if the current thread cannot create a thread in the specified thread group.

Public methods

activeCount

Added in API level 1
open fun activeCount(): Int

Returns an estimate of the number of active threads in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group.

The value returned is only an estimate because the number of threads may change dynamically while this method traverses internal data structures, and might be affected by the presence of certain system threads. This method is intended primarily for debugging and monitoring purposes.

Return
Int an estimate of the number of active threads in this thread group and in any other thread group that has this thread group as an ancestor

activeGroupCount

Added in API level 1
open fun activeGroupCount(): Int

Returns an estimate of the number of active groups in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group.

The value returned is only an estimate because the number of thread groups may change dynamically while this method traverses internal data structures. This method is intended primarily for debugging and monitoring purposes.

Return
Int the number of active thread groups with this thread group as an ancestor

allowThreadSuspension

Added in API level 1
Deprecated in API level 15
open fun allowThreadSuspension(b: Boolean): Boolean

Deprecated: The definition of this call depends on suspend, which is deprecated. Further, the behavior of this call was never specified.

Used by VM to control lowmem implicit suspension.

Parameters
b Boolean: boolean to allow or disallow suspension
Return
Boolean true on success

checkAccess

Added in API level 1
fun checkAccess(): Unit

Determines if the currently running thread has permission to modify this thread group.

If there is a security manager, its checkAccess method is called with this thread group as its argument. This may result in throwing a SecurityException.

Exceptions
java.lang.SecurityException if the current thread is not allowed to access this thread group.

destroy

Added in API level 1
fun destroy(): Unit

Destroys this thread group and all of its subgroups. This thread group must be empty, indicating that all threads that had been in this thread group have since stopped.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

Exceptions
java.lang.IllegalThreadStateException if the thread group is not empty or if the thread group has already been destroyed.
java.lang.SecurityException if the current thread cannot modify this thread group.

enumerate

Added in API level 1
open fun enumerate(list: Array<Thread!>!): Int

Copies into the specified array every active thread in this thread group and its subgroups.

An invocation of this method behaves in exactly the same way as the invocation

enumerate(list, true)
Parameters
list Array<Thread!>!: an array into which to put the list of threads
Return
Int the number of threads put into the array
Exceptions
java.lang.SecurityException if checkAccess determines that the current thread cannot access this thread group

enumerate

Added in API level 1
open fun enumerate(
    list: Array<Thread!>!,
    recurse: Boolean
): Int

Copies into the specified array every active thread in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread in these subgroups are also included. If the array is too short to hold all the threads, the extra threads are silently ignored.

An application might use the activeCount method to get an estimate of how big the array should be, however if the array is too short to hold all the threads, the extra threads are silently ignored. If it is critical to obtain every active thread in this thread group, the caller should verify that the returned int value is strictly less than the length of list.

Due to the inherent race condition in this method, it is recommended that the method only be used for debugging and monitoring purposes.

Parameters
list Array<Thread!>!: an array into which to put the list of threads
recurse Boolean: if true, recursively enumerate all subgroups of this thread group
Return
Int the number of threads put into the array
Exceptions
java.lang.SecurityException if checkAccess determines that the current thread cannot access this thread group

enumerate

Added in API level 1
open fun enumerate(list: Array<ThreadGroup!>!): Int

Copies into the specified array references to every active subgroup in this thread group and its subgroups.

An invocation of this method behaves in exactly the same way as the invocation

enumerate(list, true)
Parameters
list Array<ThreadGroup!>!: an array into which to put the list of thread groups
Return
Int the number of thread groups put into the array
Exceptions
java.lang.SecurityException if checkAccess determines that the current thread cannot access this thread group

enumerate

Added in API level 1
open fun enumerate(
    list: Array<ThreadGroup!>!,
    recurse: Boolean
): Int

Copies into the specified array references to every active subgroup in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread group in these subgroups are also included.

An application might use the activeGroupCount method to get an estimate of how big the array should be, however if the array is too short to hold all the thread groups, the extra thread groups are silently ignored. If it is critical to obtain every active subgroup in this thread group, the caller should verify that the returned int value is strictly less than the length of list.

Due to the inherent race condition in this method, it is recommended that the method only be used for debugging and monitoring purposes.

Parameters
list Array<ThreadGroup!>!: an array into which to put the list of thread groups
recurse Boolean: if true, recursively enumerate all subgroups
Return
Int the number of thread groups put into the array
Exceptions
java.lang.SecurityException if checkAccess determines that the current thread cannot access this thread group

getMaxPriority

Added in API level 1
fun getMaxPriority(): Int

Returns the maximum priority of this thread group. Threads that are part of this group cannot have a higher priority than the maximum priority.

Return
Int the maximum priority that a thread in this thread group can have.

See Also

getName

Added in API level 1
fun getName(): String!

Returns the name of this thread group.

Return
String! the name of this thread group.

getParent

Added in API level 1
fun getParent(): ThreadGroup!

Returns the parent of this thread group.

First, if the parent is not null, the checkAccess method of the parent thread group is called with no arguments; this may result in a security exception.

Return
ThreadGroup! the parent of this thread group. The top-level thread group is the only thread group whose parent is null.
Exceptions
java.lang.SecurityException if the current thread cannot modify this thread group.

interrupt

Added in API level 1
fun interrupt(): Unit

Interrupts all threads in this thread group.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

This method then calls the interrupt method on all the threads in this thread group and in all of its subgroups.

Exceptions
java.lang.SecurityException if the current thread is not allowed to access this thread group or any of the threads in the thread group.

isDaemon

Added in API level 1
fun isDaemon(): Boolean

Tests if this thread group is a daemon thread group. A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

Return
Boolean true if this thread group is a daemon thread group; false otherwise.

isDestroyed

Added in API level 1
open fun isDestroyed(): Boolean

Tests if this thread group has been destroyed.

Return
Boolean true if this object is destroyed

list

Added in API level 1
open fun list(): Unit

Prints information about this thread group to the standard output. This method is useful only for debugging.

parentOf

Added in API level 1
fun parentOf(g: ThreadGroup!): Boolean

Tests if this thread group is either the thread group argument or one of its ancestor thread groups.

Parameters
g ThreadGroup!: a thread group.
Return
Boolean true if this thread group is the thread group argument or one of its ancestor thread groups; false otherwise.

resume

Added in API level 1
Deprecated in API level 15
fun resume(): Unit

Deprecated: This method is used solely in conjunction with Thread.suspend and ThreadGroup.suspend, both of which have been deprecated, as they are inherently deadlock-prone. See Thread.suspend for details.

Resumes all threads in this thread group.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

This method then calls the resume method on all the threads in this thread group and in all of its sub groups.

Exceptions
java.lang.SecurityException if the current thread is not allowed to access this thread group or any of the threads in the thread group.

setDaemon

Added in API level 1
fun setDaemon(daemon: Boolean): Unit

Changes the daemon status of this thread group.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

Parameters
daemon Boolean: if true, marks this thread group as a daemon thread group; otherwise, marks this thread group as normal.
Exceptions
java.lang.SecurityException if the current thread cannot modify this thread group.

setMaxPriority

Added in API level 1
fun setMaxPriority(pri: Int): Unit

Sets the maximum priority of the group. Threads in the thread group that already have a higher priority are not affected.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

If the pri argument is less than Thread.MIN_PRIORITY or greater than Thread.MAX_PRIORITY, it is clamped to those values.

Otherwise, the priority of this ThreadGroup object is set to the smaller of the specified pri and the maximum permitted priority of the parent of this thread group. (If this thread group is the system thread group, which has no parent, then its maximum priority is simply set to pri.) Then this method is called recursively, with pri as its argument, for every thread group that belongs to this thread group.

Parameters
pri Int: the new priority of the thread group.
Exceptions
java.lang.SecurityException if the current thread cannot modify this thread group.

stop

Added in API level 1
Deprecated in API level 15
fun stop(): Unit

Deprecated: This method is inherently unsafe. See java.lang.Thread#stop for details.

Stops all threads in this thread group.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

This method then calls the stop method on all the threads in this thread group and in all of its subgroups.

Exceptions
java.lang.SecurityException if the current thread is not allowed to access this thread group or any of the threads in the thread group.

suspend

Added in API level 1
Deprecated in API level 15
fun suspend(): Unit

Deprecated: This method is inherently deadlock-prone. See Thread.suspend for details.

Suspends all threads in this thread group.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

This method then calls the suspend method on all the threads in this thread group and in all of its subgroups.

Exceptions
java.lang.SecurityException if the current thread is not allowed to access this thread group or any of the threads in the thread group.

toString

Added in API level 1
open fun toString(): String

Returns a string representation of this Thread group.

Return
String a string representation of this thread group.

uncaughtException

Added in API level 1
open fun uncaughtException(
    t: Thread,
    e: Throwable
): Unit

Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

The uncaughtException method of ThreadGroup does the following:

  • If this thread group has a parent thread group, the uncaughtException method of that parent is called with the same two arguments.
  • Otherwise, this method checks to see if there is a uncaught exception handler installed, and if so, its uncaughtException method is called with the same two arguments.
  • Otherwise, this method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's java.lang.Throwable#printStackTrace method, is printed to the standard error stream.

Applications can override this method in subclasses of ThreadGroup to provide alternative handling of uncaught exceptions.

Parameters
t Thread: the thread that is about to exit.
e Throwable: the uncaught exception.