JavaScript is Single-Threaded
Index
- Single-Threaded का मतलब क्या है?
- Example: Simple Code Execution
- Blocking Code (Blocking Behavior in Single-Threaded JS)
- How Does JavaScript Handle Asynchronous Tasks?
- Single-Threaded vs Multi-Threaded
- Key Points (मुख्य बातें)
- Conclusion
Single-Threaded का मतलब क्या है?
Single-threaded होने का मतलब यह है कि JavaScript का execution एक ही thread में होता है, और यह thread sequentially, यानी एक के बाद एक, commands को process करता है।
Example: Simple Code Execution
function firstTask() {
console.log("This is the first task.");
}
function secondTask() {
console.log("This is the second task.");
}
firstTask();
secondTask();
Execution Flow (Flow of the Single Thread):
- JavaScript सबसे पहले
firstTask()
को execute करेगा। - जब यह complete हो जाएगा, तब
secondTask()
execute होगा। - कोई भी दूसरा code तब तक execute नहीं होगा जब तक कि current task (function) पूरा न हो जाए।
Output:
This is the first task.
This is the second task.
Blocking Code (Blocking Behavior in Single-Threaded JS)
Single-threaded होने की वजह से अगर कोई task बहुत समय ले रहा है (जैसे कि कोई heavy computation), तो वह दूसरे tasks को block कर देगा। यानी बाकी tasks तब तक wait करेंगे जब तक current task complete नहीं हो जाता।
Example: Blocking Code
function longTask() {
for (let i = 0; i < 1e9; i++) {} // This is a time-consuming task
console.log("Long task complete.");
}
function shortTask() {
console.log("Short task complete.");
}
longTask(); // First execute the long task
shortTask(); // Then execute the short task (after long task finishes)
Output:
Long task complete.
Short task complete.
Explanation: यहां longTask()
पूरा होने में समय लेगा, इसलिए जब तक यह execute हो रहा है, बाकी का code wait करेगा। यह blocking behavior कहलाता है।
How Does JavaScript Handle Asynchronous Tasks?
JavaScript में भले ही single-threaded environment हो, लेकिन asynchronous tasks (जैसे कि HTTP requests, setTimeout) को handle करने के लिए Event Loop और Callback Queue का use किया जाता है। इसका मतलब है कि JavaScript दूसरे background tasks (जैसे network requests) को asynchronous तरीके से handle करता है, और जब यह tasks complete होते हैं, तो इन्हें call stack में वापस लाया जाता है।
Example: Asynchronous Code (Non-blocking Example)
console.log("Start");
setTimeout(() => {
console.log("This is an asynchronous task.");
}, 2000);
console.log("End");
Output:
Start
End
This is an asynchronous task.
Single-Threaded vs Multi-Threaded
- Single-Threaded: JavaScript में एक ही thread होता है, यानी एक ही समय में सिर्फ एक task execute हो सकता है।
- Multi-Threaded: दूसरी languages (जैसे Java, C++) multi-threading को support करती हैं, जहां एक ही समय में multiple threads parallel में tasks को execute कर सकते हैं। JavaScript ऐसा नहीं करता, लेकिन asynchronous tasks को efficiently handle कर सकता है।
Key Points (मुख्य बातें)
- Single-threaded होने के कारण JavaScript एक समय में सिर्फ एक task execute करता है।
- अगर कोई function बहुत समय लेता है (जैसे heavy computation), तो वह बाकी code execution को block कर सकता है।
- Asynchronous tasks (जैसे कि network requests या timers) को handle करने के लिए JavaScript Event Loop का इस्तेमाल करता है।
- Single-threaded होने के बावजूद, JavaScript asynchronous programming का use करके efficient performance देता है।
Conclusion
JavaScript एक single-threaded language है, यानी यह एक समय में एक ही task को execute करता है। इसका मतलब है कि सभी tasks sequentially execute होते हैं। हालांकि, asynchronous tasks को efficiently handle करने के लिए JavaScript के पास Event Loop और Callback Queue जैसी powerful mechanisms हैं, जो इसे non-blocking और responsive बनाते हैं।
No comments:
Post a Comment