Skip to content

Commit 209adcf

Browse files
authored
Update Things-To-Know.md
1 parent 2214baa commit 209adcf

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

Things-To-Know.md

+178
Original file line numberDiff line numberDiff line change
@@ -1 +1,179 @@
1+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2+
# Internal Working
13

4+
## When we call the start() method on a thread, the JVM schedules the thread for execution on a logical processor, which can be a physical core or a hyper-threaded logical core in our CPU.
5+
6+
## CPU Structure
7+
### Physical Cores:
8+
9+
These are the actual hardware components in the CPU that perform computation.
10+
Each physical core can execute one thread at a time.
11+
12+
### Logical Processors:
13+
14+
Modern CPUs often use a technology called Hyper-Threading (by Intel) or Simultaneous Multithreading (SMT) (by AMD).
15+
This technology allows each physical core to be split into multiple logical processors.
16+
Each logical processor can handle its own thread, giving the appearance of more cores to the operating system and applications.
17+
18+
19+
## Example
20+
### 4-core CPU with Hyper-Threading:
21+
22+
A CPU with 4 physical cores might support Hyper-Threading, providing 8 logical processors.
23+
This means the CPU can handle 8 threads simultaneously.
24+
Execution Flow
25+
26+
Thread Start:
27+
28+
You call start() on a thread, notifying the JVM that the thread is ready to run.
29+
JVM Scheduler:
30+
31+
The JVM's thread scheduler decides when and on which logical processor the thread will run.
32+
Core Execution:
33+
34+
The thread is executed on one of the logical processors.
35+
The logical processor could be a physical core or a hyper-threaded logical core.
36+
37+
```
38+
39+
40+
class MyRunnable implements Runnable {
41+
@Override
42+
public void run() {
43+
System.out.println("Thread is running on: " + Thread.currentThread().getName());
44+
}
45+
}
46+
47+
public class Main {
48+
public static void main(String[] args) {
49+
Thread thread1 = new Thread(new MyRunnable());
50+
Thread thread2 = new Thread(new MyRunnable());
51+
thread1.start();
52+
thread2.start();
53+
}
54+
}
55+
56+
```
57+
### What Happens Internally
58+
Thread Initialization: new Thread(new MyRunnable()) creates a new thread object.
59+
Thread Scheduling: thread1.start() and thread2.start() notify the JVM scheduler.
60+
Core Execution: The JVM maps thread1 and thread2 to available logical processors for execution.
61+
Run Method: The cores (or logical processors) execute the run() method of MyRunnable.
62+
63+
### Summary
64+
Physical Cores: Actual hardware units that perform computation.
65+
Logical Processors: Virtual cores created by technologies like Hyper-Threading, allowing multiple threads per physical core.
66+
When you call start(), the JVM schedules the thread to run on a logical processor.
67+
The logical processor could be a physical core or a hyper-threaded core.
68+
69+
In modern CPUs, this allows better utilization of CPU resources and can improve performance by allowing more threads to run concurrently.
70+
71+
72+
73+
74+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
75+
# Key Components of a CPU
76+
## Cores:
77+
78+
1. Physical Cores: These are the actual hardware units that perform computation. They execute instructions from the computer’s programs.
79+
2. Logical Processors: Created by technologies like Hyper-Threading (Intel) or Simultaneous Multithreading (SMT) (AMD). Each physical core can be divided into multiple logical processors, allowing more threads to be handled simultaneously.
80+
81+
## Cache Memory:
82+
83+
L1 Cache: The smallest and fastest cache, located directly on the CPU core. It stores frequently accessed data and instructions.
84+
L2 Cache: Larger than L1, but slower. It also stores frequently accessed data and can serve multiple cores.
85+
L3 Cache: The largest and slowest cache, shared across all cores in a CPU. It helps reduce the latency for memory accesses.
86+
87+
## Arithmetic Logic Unit (ALU):
88+
89+
The ALU performs arithmetic and logical operations (such as addition, subtraction, and comparisons).
90+
## Floating Point Unit (FPU):
91+
92+
The FPU handles complex mathematical calculations, particularly those involving floating-point numbers.
93+
## Control Unit (CU):
94+
95+
The CU directs the operation of the processor. It tells the ALU, memory, and I/O devices how to respond to the instructions received from the program.
96+
## Registers:
97+
98+
Small, fast storage locations within the CPU used to hold data that is being processed or used immediately. Examples include the instruction register, accumulator, and program counter.
99+
## Bus Interface Unit (BIU):
100+
101+
The BIU manages the data transfer between the CPU and the system’s memory or input/output devices. It includes address buses, data buses, and control buses.
102+
## Instruction Decoder:
103+
104+
This component interprets the instructions fetched into the CPU, determining which operations the ALU and other components should perform.
105+
## Clock:
106+
107+
The clock synchronizes the operations of the CPU by generating a continuous sequence of electrical pulses. The frequency of these pulses (measured in GHz) determines the speed at which the CPU can process instructions.
108+
## Memory Management Unit (MMU):
109+
110+
The MMU handles the translation of virtual memory addresses to physical addresses. It manages the memory hierarchy and paging mechanisms.
111+
## Additional CPU Features
112+
## Integrated Graphics Processing Unit (GPU): Some CPUs come with an integrated GPU to handle graphics processing tasks.
113+
## Thermal Management: Modern CPUs include mechanisms for managing heat, such as thermal sensors and throttling capabilities.
114+
## Security Features: CPUs often include hardware-based security features like Intel’s SGX (Software Guard Extensions) or AMD’s SEV (Secure Encrypted Virtualization).
115+
## Summary
116+
The CPU is a complex and highly integrated component comprising physical cores, logical processors, cache memory, the ALU, FPU, control unit, registers, BIU, instruction decoder, clock, and MMU. These components work together to execute instructions and process data efficiently. Understanding these parts helps appreciate how a CPU handles multiple tasks, such as those in multithreading and parallel processing.
117+
118+
119+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
120+
121+
122+
123+
When we call the start() method on a Thread in Java, the CPU and its components play specific roles to handle the execution of the thread. Here’s a step-by-step breakdown of what happens and how the CPU components are involved:
124+
125+
1. Calling start() Method
126+
When we call the start() method on a thread, the following happens:
127+
128+
A new thread is created in the JVM.
129+
The thread is registered with the operating system.
130+
The run() method of the thread is scheduled to be executed by the JVM.
131+
2. Operating System Scheduler
132+
The operating system’s scheduler is responsible for managing which threads run on which cores or logical processors. It schedules the threads based on their priority and the current workload of the system.
133+
134+
3. Thread Allocation to CPU Core
135+
The operating system assigns the thread to an available logical processor (which could be one of the multiple threads on a physical core).
136+
137+
4. CPU Core Execution
138+
Once assigned to a core or logical processor, the following CPU components are involved in executing the thread:
139+
140+
a. Instruction Fetch and Decode
141+
Instruction Decoder: The CPU fetches instructions from the memory (via the instruction register) and decodes them to understand what actions to perform.
142+
Control Unit (CU): Directs the execution of these instructions, telling the ALU, FPU, and other components what to do.
143+
b. Execution
144+
Arithmetic Logic Unit (ALU): Performs arithmetic and logical operations as required by the thread.
145+
Floating Point Unit (FPU): Handles complex mathematical calculations if needed by the thread.
146+
c. Memory Access
147+
Cache Memory (L1, L2, L3): The CPU uses its cache to quickly access frequently used data and instructions.
148+
Memory Management Unit (MMU): Translates virtual addresses to physical addresses, ensuring the correct data is accessed from the main memory.
149+
d. Data Transfer
150+
Registers: Temporary storage within the CPU where data and instructions currently being used are held.
151+
Bus Interface Unit (BIU): Manages the data transfer between the CPU and the system’s memory or I/O devices.
152+
153+
5. Multithreading and Parallel Execution
154+
If the CPU has multiple cores and supports multithreading, several threads can run in parallel. Here’s how:
155+
156+
Physical Cores: Each physical core can handle one or more threads at the same time.
157+
Logical Processors: Technologies like Hyper-Threading allow a single physical core to handle multiple threads concurrently by providing additional logical processors.
158+
6. Context Switching
159+
If the CPU is running multiple threads on the same core (logical or physical), it will perform context switching:
160+
161+
Save State: The current state of a thread is saved (registers, program counter, etc.).
162+
Load State: The state of the next thread to run is loaded.
163+
Execution Resumes: The new thread resumes execution.
164+
7. Completion and Shutdown
165+
Once a thread completes its execution:
166+
167+
The CPU stops executing its instructions.
168+
The operating system scheduler may assign the logical processor to another thread.
169+
If the thread pool or executor service is being used, the thread is returned to the pool for reuse.
170+
171+
172+
Summary
173+
Instruction Fetch and Decode: Fetch and decode instructions from the thread.
174+
Execution: Perform arithmetic, logical, and floating-point operations.
175+
Memory Access: Use caches and MMU for fast and correct memory access.
176+
Data Transfer: Manage data transfers via registers and buses.
177+
Multithreading: Execute multiple threads in parallel using cores and logical processors.
178+
Context Switching: Save and load thread states to handle multiple threads on a single core.
179+
Completion: Threads complete, and resources are freed or reused

0 commit comments

Comments
 (0)