Thread and Runnable
The Java API includes two classes
that embody the core thread support in the language. These classes
are java.lang.Thread and
java.lang.Runnable. They allow you to define
threads of control in your application, and to manage threads in
terms of runtime resources and running state.
As the name suggests, java.lang.Thread
represents a thread of control. It offers methods that allow you to
set the priority of the thread, to assign a thread to a thread group
(more on these in a later section), and to control the running state
of the thread (e.g., whether it is running or suspended).
The java.lang.Runnable interface represents the
body of a thread. Classes that implement the
Runnable interface provide their own
run()
methods that determine what
their thread actually does while running. In fact,
run() is the only method defined by the
Runnable interface. If a
Thread is constructed with a
Runnable object as its body, the
run() method on the
Runnable will be called when the thread is
started.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access