Summary

Here’s a list of the methods of the Thread class that we introduced in this chapter:

Thread()

Constructs a thread object using default values for all options.

Thread(Runnable target)

Constructs a new thread object associated with the given Runnable object.

Thread(String name)

Constructs a thread object with a name that is already assigned. This constructor is used when threading by inheritance.

Thread(Runnable target, String name)

Constructs a thread object that is associated with the given Runnable object and is created with a name that is already assigned. This constructor is used when threading by interfaces.

void run()

The method that the newly created thread will execute. Developers should override this method with the code they want the new thread to run; we’ll show the default implementation of the run() method a little further on, but it is essentially an empty method.

void start()

Creates a new thread and executes the run() method defined in this thread class.

void stop() (deprecated in Java 2)

Terminates an already running thread.

static void sleep (long milliseconds)

Puts the currently executing thread to sleep for the specified number of milliseconds. This method is static and may be accessed through the Thread class name.

static void sleep (long milliseconds, int nanoseconds)

Puts the currently executing thread to sleep for the specified number of milliseconds and nanoseconds. This method is static and may be accessed through the Thread class name.

Get Java Threads, Second Edition 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.