C
C
Core Java
Search…
Threads
All nontrivial Java applications are multithreaded. A JVM supports multiple thread execu- tion. Multiple threads can be supported by an underlying system by using multiple hard- ware processors, by time-slicing a single processor, or by time-slicing multiple processors. Java language supports multithreading. Class Thread and the Runnable interface can be used to define and start your own threads. In the next chapter on concurrency, we’ll explore the java.util.concurrent package to execute your threads.
You’ll often hear about thread objects and threads of execution in a multithreaded or concurrent application. Though related, a Thread instance and a thread of execution aren’t the same. A Thread instance is a Java object. The implementation of Java threads is JVM-specific. A JVM implementation might even choose not to map Java threads to native threads at all! Creation of a thread of execution is passed to the OS by a JVM implementation. A thread of execution has its own set of program counter (PC) registers to store the next set of instructions to execute. It also has its own stack that stores method frames to store the state of a method invocation. The state of a method invocation includes the value of local variables, method parameters, method return values, exception handler parameters, and intermediate values. A process like JVM’s can create multiple threads of execution to execute multiple tasks simultaneously. A Thread instance and a thread of execution are depicted in figure.
Have you ever wondered how many threads your Java application has? At least one—the main thread. A JVM starts execution of a Java application using a thread of execution: main. This thread executes code defined in method main() and can create other threads of execution.
Last modified 2yr ago
Copy link