Generated by GPT-5-mini| java.lang.Thread | |
|---|---|
| Name | Thread |
| Package | java.lang |
| Introduced | Java 1.0 |
| Related | Runnable, ThreadGroup, ThreadLocal, Executor, ForkJoinPool |
java.lang.Thread
java.lang.Thread is the standard Java class used to represent and control an execution thread within the Java Virtual Machine. It integrates with the Java concurrency model provided by the Java Platform and interoperates with APIs in the Java Standard Library, the Java Memory Model, and operating system thread facilities exposed via the Java Native Interface. Implementations of java.lang.Thread are used throughout frameworks from Apache to Oracle products and underpin concurrency in server platforms such as Tomcat and Spring.
java.lang.Thread enables concurrent execution by encapsulating a thread of control and associating it with a stack, program counter, and native scheduling entity. It collaborates with interfaces and classes including Runnable, Callable (Java), ThreadGroup, ThreadLocal, and implementations of ExecutorService and ForkJoinPool to provide structured concurrency primitives. The behavior of threads is defined by the Java Memory Model specified in the Java Language Specification and influences portability across implementations like OpenJDK, HotSpot, and other JVMs used by projects such as OpenJDK and Eclipse OpenJ9.
Threads can be created by subclassing the class and overriding the run method or by passing a Runnable or Callable (Java) wrapped in a FutureTask to a Thread constructor. Common patterns include: new Thread(new Runnable() { ... }), Thread t = new Thread(runnable, "name"); and using factory methods from Executors to obtain thread pools rather than creating raw threads. High-level frameworks such as Spring Framework, Akka (toolkit), and Netty typically prefer ExecutorService or managed dispatchers to control lifecycle and resource allocation across platforms like IBM JDK and Oracle JDK.
The lifecycle of a thread is represented by states defined in java.lang.Thread: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. Transitions occur via methods such as start(), sleep(), wait(), join(), interrupt(), and Object monitor interactions, and are affected by synchronization constructs in the Java Memory Model. System monitoring tools and profilers such as VisualVM, JConsole, and Flight Recorder visualize thread states in JVMs like HotSpot and OpenJ9, and tracing utilities in Linux and Windows can map JVM threads to native kernel threads.
Synchronization for threads relies on intrinsic monitors (synchronized blocks) and explicit locks like ReentrantLock from the java.util.concurrent.locks package. Higher-level utilities include Semaphore (programming), CountDownLatch, CyclicBarrier, Phaser, ConcurrentHashMap, and BlockingQueue implementations used by producer–consumer patterns. Frameworks and libraries such as Guava (software), Akka (toolkit), and Hazelcast build on these primitives to provide distributed coordination, while language specifications and JVM implementations ensure happens-before relationships required by the Java Memory Model.
Thread scheduling and priority are advisory hints mediated by the underlying operating system scheduler; Thread#setPriority interacts with native thread priorities on platforms like Linux, FreeBSD, macOS, and Windows NT. Policies such as preemptive multitasking and time-slicing used by kernels like Linux kernel affect fairness and throughput. Real-time JVMs and standards like Real-Time Specification for Java target different scheduling semantics for embedded and real-time systems, and container orchestration platforms such as Docker and Kubernetes can influence CPU allocation and effective thread scheduling.
Unhandled exceptions thrown from a thread's run method terminate that thread and are reported via the thread’s UncaughtExceptionHandler or the ThreadGroup default handler. Applications and frameworks register handlers to log or recover from failures; logging systems like Log4j, java.util.logging, and SLF4J are commonly used to record uncaught exceptions. Container runtimes such as Tomcat and Jetty often provide policies to manage thread pool faults, while reactive systems like Reactive Streams and Project Reactor favor functional error propagation over raw thread-level exception handling.
Prefer managed executors and thread pools from Executors over creating raw threads to avoid resource exhaustion and to enable tuning via tools like JVM tuning guides and profilers such as YourKit and VisualVM. Use immutable data and ThreadLocal sparingly, prefer lock-free algorithms and classes like ConcurrentHashMap for scalability, and minimize blocking operations to reduce context-switch overhead on platforms such as Linux and Solaris. Consider using higher-level concurrency frameworks—CompletableFuture, ForkJoinPool, and reactive libraries—to express asynchronous workflows and improve throughput in server frameworks like Spring Boot and Micronaut.
Category:Java APIs Category:Concurrency