Java Concurrency - Thread

Java Conceptuel Diagram

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.

Properties

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

State

javase/9/docs/api/java/lang/Thread.html will return a javase/9/docs/api/java/lang/Thread.State.html

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

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.

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.

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

Example: A lambda expression of a runnable used in the argument of Thread.

new Thread(()->{
    IntStream.range(0, 50).forEach(i -> {
        System.out.println(i);
    });
}).start();

Executor

See Java Concurrency - Executor (Thread manager)

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.

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.

Monitoring

Java Mission Control

Thread Java Mission Control

Documentation / Reference





Discover More
Data System Architecture
Concurrency - Thread Interference (Interleave on shared data)

Same name than ? how errors are introduced when multiple threads access shared data. Interference happens when two operations, running in different threads, but acting on the same data, interleave....
Java Conceptuel Diagram
Java - Concurrency (Parallel Work)

In concurrent or parallel programming, there are two basic units of execution: processes. A process is implemented as a main thread that can create other thread. and threads. Threads exist within...
Java Conceptuel Diagram
Java - Runnable

A java/lang/RunnableRunnable is a configuration object that define the code that will run inside threads. See You define the code to be run in the method, java/lang/Runnablerun You pass the Runnable...
Java Conceptuel Diagram
Java - Thread Stack (Stack Trace)

thread stack (ie stack trace) Jstack A core dump has also the stack trace The -m option of Jstack can be used to print the native frames
Java Conceptuel Diagram
Java Concurrency - (Object) Wait

in Java. java/lang/ObjectObject.wait, java/lang/Object.htmlwait(long timeout) and java/lang/Objectwait(long timeout, int nanos) suspend the current thread. The invocation of wait does not return until...
Java Conceptuel Diagram
Java Concurrency - Deadlock

deadlock in Java. The Java programming language neither prevents nor requires detection of deadlock conditions. Programs where threads hold (directly or indirectly) locks on multiple objects should...
Java Conceptuel Diagram
Java Concurrency - Executor (Thread manager)

An executor is a thread management module (thread creation and management). java.util.concurrent provides two executor interfaces: java/util/concurrent/ExecutorExecutor is a simple standardized...
Java Conceptuel Diagram
Java Concurrency - Process (Main thread)

process in Java A process in Java is: started by a main class. implemented as a main thread that can create other thread. It has: a self-contained execution environment. a complete, private...
Java Conceptuel Diagram
Java Concurrency - Synchronization (Thread Safety)

in java. Java offers two basic synchronization idioms: synchronized methods synchronized statements final fields, which cannot be modified after the object is constructed, can be safely read...
Java Conceptuel Diagram
Java Concurrency - Thread Dump

A thread dump consists of: an thread header (followed by the thread stack) the thread stack, including thread state, for all Java threads in the virtual machine. The thread dump does not terminate...



Share this page:
Follow us:
Task Runner