The Java programming language and the Java virtual machine (JVM) are designed to support concurrent programming. All execution takes place in the context of threads. Objects and resources can be accessed by many separate threads, with each thread with its own path of execution, but with access to any object in the program. Read and write access to objects must be properly coordinated (or "synchronized") between threads. Thread synchronization ensures that objects are modified by only one thread at a time and prevents threads from accessing partially updated objects during modification by another thread.
Processes and threads Most implementations of the Java virtual machine run as a single process. In the Java programming language, concurrent programming is primarily concerned with threads (also called lightweight processes). Multiple processes can only be realized with multiple JVMs.
Thread objects Threads share the process' resources, including memory and open files. This makes for efficient, but potentially problematic, communication. Every application has at least one thread called the main thread. The main thread has the ability to create additional threads as java.lang.Runnable or java.util.concurrent.Callable objects. The java.lang.Callable interface is similar to java.lang.Runnable in that both are designed for classes whose instances are potentially executed by another thread. A java.lang.Runnable, however, does not return a result and cannot throw a checked exception. Each thread can be scheduled on a different CPU core or use time-slicing on a single hardware processor, or time-slicing on many hardware processors. There is no general solution to how Java threads are mapped to native OS threads. Every JVM implementation can do this differently. Each thread is associated with an instance of the class java.lang.Thread. Threads can be managed either by directly using the java.lang.Thread objects, or indirectly by using abstract mechanisms such as java.util.concurrent.Executors or task classes. Java provides the following task classes:
java.util.concurrent.ForkJoinTask, and its descendants: java.util.concurrent.CountedCompleter java.util.concurrent.RecursiveAction (extends ForkJoinTask<Void>) java.util.concurrent.RecursiveTask java.util.concurrent.FutureTask javafx.concurrent.Task<V> (from JavaFX) As part of Project Loom in Java 21, virtual threads were introduced, which allow for normal, blocking, synchronous code while scaling to millions of tasks. The classic Java thread is called a "platform thread", corresponding roughly to an OS thread, but Java virtual threads are lightweight and managed by the JVM, scheduled onto a small pool of real OS threads. Virtual threads are still java.lang.Thread, but are created with factory methods like ofVirtual(). Although the code is still synchronous and blocking, the physical OS threads are asynchronous; upon stopping to wait, the JVM detaches the code from the physical thread, running other tasks until the OS sends an asynchronous event notification to pick up the code where it left off to finish.
Thread starting A thread may be started through providing a java.lang.Runnable object:
Or, through inheritance from java.lang.Thread:
Interrupts An interrupt tells a thread that it should stop what it is doing and do something else. A thread sends an interrupt by invoking interrupt() on a java.lang.Thread for the thread to be interrupted. The interrupt mechanism is implemented using an internal flag marking its interruption status, which is set upon a call to interrupt(). Any method that exits by throwing a java.lang.InterruptedException clears the interrupted status when it does so, but there is the possibility that the interrupted status may immediately be set again, by another thread calling interrupt(). Historically, threads could be stopped immediately using the stop() method; upon calling this, a java.lang.ThreadDeath would be thrown, which clears the thread. This method was dangerous as it could leave data in a corrupted state, and was eventually removed in Java 20.
Joins The java.lang.Thread#join() method allows a java.lang.Thread to wait for the completion of another.
Exceptions Uncaught exceptions thrown by code will terminate the thread. The main thread prints exceptions to the console, but user-created threads need a handler registered to do so (see java.lang.Thread.UncaughtExceptionHandler).
Memory model The Java memory model describes how threads in the Java programming language interact through memory. On modern platforms, code is frequently not executed in the order it was written. It is reordered by the compiler, the processor and the memory subsystem for performance reasons. There is no guarantee of linearizability, or even sequential consistency, when reading or writing fields of shared objects. This allows for compiler optimizations (such as register allocation, common subexpression elimination, and redundant read elimination), through reordering memory read-writes.
… excerpt ends here. Continue reading the full article.