# JAVA THREAD
If there is a Java file named **Main.java**,
```text
Main.java → javac → Main.class
Main.class is a file.
When we execute Main.class:
java Main
The operating system loads Main.class into memory, and it becomes a process.
💡 What happens when you run a Java program?
- The OS creates a new process for the Java program (here, it is
Main).- The JVM starts inside that process.
- The JVM creates one thread automatically.
- This first thread is called the Main Thread.
- The Main Thread executes the
main()method.
Process
A process contains:
- Code
- Heap
- Stack
- Registers
- Threads
💡 Thread
- A thread is the smallest unit of execution inside a process.
- Java starts with one thread. Every Java program already has one thread. It means that the
main()function is running inside the Main Thread.A thread is an execution path. It has:
- Stack
- Program Counter
- Registers
- Thread State
A thread can execute code independently.
A process has its own memory:
-
Code Segment (
.class, JVM code) - Heap (shared by all threads)
- Method Area (shared)
- Stack (Main Thread)
- Registers (Main Thread)
- Program Counter (Main Thread)
Suppose we create another thread, and when start() is called, the JVM asks the OS:
"Please create another thread inside the same process."
The OS does not create another process when a new thread is created.
Instead, it creates another execution path inside the same process.
After creating a new thread called Worker, the memory looks like this:
- Code Segment (Shared) → There is only one copy of the compiled code.
- Heap (Shared)
- Method Area (Shared) → Contains class information, static variables, and bytecode.
- Main Thread Stack
- Worker Thread Stack
- Main Thread Registers
- Worker Thread Registers
- Main Thread Program Counter
- Worker Thread Program Counter
Context Switching
A context switch is the process where the operating system stops executing one thread (or process), saves its execution state, and restores the state of another thread (or process) so that execution can continue.
💡 Is a thread stopped in the middle of a Java statement?
Yes.
Every single Java statement is compiled into multiple CPU instructions.
The CPU can be interrupted between these machine instructions.
This is one reason why operations that look simple in Java are not always atomic.
An atomic operation is an operation that executes completely or doesn't execute at all. It can't be interrupted in the middle.
Triggers for Context Switch
-
Timer Interrupt
- Gives each thread a time slice (called preemptive multitasking).
-
Blocking I/O
- Example:
Scanner sc = new Scanner(System.in);If the user doesn't type anything, instead of wasting CPU time, the OS switches to another thread.
-
Sleep
- The thread voluntarily gives up the CPU.
-
Waiting for Lock
- If another thread already owns the lock, the current thread waits.
- The CPU switches elsewhere.
Higher Priority Thread
Context Switching Process
CPU
│
▼
Running Thread A
│
Timer Interrupt
│
▼
Enter Kernel Mode
(Only the kernel can perform scheduling)
│
▼
Save Thread A Context
(into Thread A's PCB/TCB)
│
▼
Scheduler Chooses
("Which thread should run next?")
│
▼
Restore Thread B Context
│
▼
Return to User Mode
│
▼
Running Thread B
Types of Context Switching
Process Context Switch
Switching between two processes or switching between threads belonging to different processes.
It is more expensive because the OS changes:
- Registers
- Program Counter
- Stack Pointer
- Virtual Memory
- Page Tables
- Address Space
Example
Suppose multiple processes are running:
- Chrome
- VS Code
- Java
Currently, the CPU is executing Chrome.
Now the scheduler decides to run the Java process.
The OS must switch everything needed to execute the Java process.
Thread Context Switch
Switching between two threads in the same process.
It is faster because only the following need to be saved and restored:
- Registers
- Program Counter
- Stack Pointer
| Feature | Thread Context Switch | Process Context Switch |
|---|---|---|
| Switch between | Threads | Processes |
| Address Space | Same | Different |
| Heap | Shared | Different |
| Code | Shared | Different |
| Stack | Different | Different |
| Registers | Saved/Restored | Saved/Restored |
| Program Counter | Saved/Restored | Saved/Restored |
| Page Table | Usually unchanged | Changed |
| Cost | Lower | Higher |
Context switching is based on what the scheduler switches the CPU to.
💡 Concurrency
Concurrency is the ability of a system to handle and make progress on multiple tasks at the same time.
The system achieves concurrency by rapidly switching between tasks or running them independently, rather than finishing one full task before starting the next.
Top comments (0)