Thread Class

Package: java.lang

The Thread class lets you create an object that can be run as a thread in a multithreaded Java application.

Note that an alternative to using the Thread class to crate multithreaded applications is to use the Runnable interface. For more information, see Runnable Interface.

Constructors

Constructor

Description

Thread()

Creates an instance of the Thread class. This constructor is the basic Thread constructor without parameters.

Thread(String name)

Creates a Thread object and assigns the specified name to the thread.

Thread(Runnable target)

Turns any object that implements an API interface called Runnable into a thread. You see how this more-advanced constructor is used later in this part.

Thread(Runnable target, String name)

Creates a thread from any object that implements Runnable and assigns the specified name to the thread.

Methods

Method

Description

static int activeCount()

Returns the number of active threads.

static int enumerate(Thread[] t)

Fills the specified array with a copy of each active thread. The return value is the number of threads added to the array.

String getName()

Returns the name of the thread.

int getPriority()

Returns the thread’s priority.

void interrupt()

Interrupts this thread.

boolean isInterrupted()

Checks whether the thread has been interrupted.

void setPriority(int priority)

Sets the thread’s priority.

void setName(String ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.