Java Concurrency - Thread

> Procedural Languages > Java > Java - Concurrency (Parallel Work)

1 - About

Every application has at least one thread .

From the application programmer's point of view, you start with just one thread, called the main thread. From this thread, you can start other threads.

Advertising

3 - Properties

3.1 - Name

When a thread is created using defaultThreadFactory object, the thread will be named pool-N-thread-M where:

  • N is the sequence number of this factory,
  • and M is the sequence number of the thread created by this factory

3.2 - State

java/lang/Thread#getState() will return a java/lang/Thread.State

Thread State Meaning
NEW The thread has not yet started.
RUNNABLE The thread is executing in the Java virtual machine.
BLOCKED The thread is blocked waiting for a monitor lock.
WAITING The thread is waiting indefinitely for another thread to perform a particular action.
TIMED_WAITING The thread is waiting for another thread to perform an action for up to a specified waiting time.
TERMINATED The thread has exited.

The thread state can also be found in a thread dump

Advertising

4 - Management

The Thread class defines a number of methods useful for thread management. These include static methods, which provide information about, or affect the status of, the thread invoking the method.

4.1 - Create

Each thread is (associated with an instance of | represented by ) the class Thread. There are two basic strategies for using Thread objects to create a concurrent application.

  • To directly control thread creation and management, simply instantiate Thread each time the application needs to initiate an asynchronous task.
  • To abstract thread management from the rest of your application, pass the application's tasks to an executor.

4.1.1 - Instantiation

An application that creates an instance of Thread must provide the code that will run in that thread.

There are two ways to do this:

  • (Implement|Provide) a Runnable object.
  • (Extend|Subclass) Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run.

The first implementation, which employs a Runnable object must be in general used because:

  • the Runnable object can subclass a class other than Thread.
  • The extension of thread is limited by the fact that your task class must be a descendant of Thread.
  • this approach more flexible
  • it is applicable to the high-level thread management APIs
Advertising

4.1.2 - Executor

4.2 - Thread.Start

When a statement invokes Thread.start, every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the creation of the new thread are visible to the new thread.

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

4.3 - Thread.join

When a thread terminates and causes a Thread.join in another thread to return, then all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join.

Ie: The parent thread will wait that the child thread terminates before continuing.

5 - Monitoring

5.1 - Java Mission Control

6 - Documentation / Reference