I/O के लिए / से परे, .NET के लिए प्रदान करता है — कई cores में computations चलाना। मुख्य अंतर: / I/O concurrency के लिए है (non-blocking waits), जबकि , , और PLINQ CPU-intensive काम को parallelize करने के लिए हैं।
I/O के लिए / से परे, .NET के लिए प्रदान करता है — कई cores में computations चलाना। मुख्य अंतर: / I/O concurrency के लिए है (non-blocking waits), जबकि , , और PLINQ CPU-intensive काम को parallelize करने के लिए हैं।
asyncawaitasyncawaitTask.RunParallelasync/await → I/O-bound concurrency. Frees the thread during waits (DB, network).
Does NOT add CPU parallelism.
Parallelism → CPU-bound work spread across MULTIPLE CORES (computation, processing).
Use Task.Run, Parallel.For/ForEach, PLINQ.
// run a CPU-intensive computation on a thread pool thread (don't block the caller)
int result = await Task.Run(() => ExpensiveComputation());
// run multiple in parallel and combine
var tasks = items.Select(item => Task.Run(() => Process(item)));
var results = await Task.WhenAll(tasks); // wait for all
Task.Run काम को एक thread pool thread पर schedule करता है — CPU-bound computation को parallelize करने के लिए इसका उपयोग करें (I/O के लिए नहीं, जहां async/await पहले से ही अतिरिक्त threads के बिना पर्याप्त है)।
// Parallel.For/ForEach — process a collection across cores
Parallel.ForEach(items, item => Process(item)); // automatically uses multiple cores
Parallel.For(0, 1000, i => Compute(i));
// PLINQ — parallel LINQ
var results = data.AsParallel().Where(x => IsValid(x)).Select(Transform).ToList();
Parallel.For/ForEach और PLINQ (.AsParallel()) स्वचालित रूप से काम को cores में वितरित करते हैं — स्वतंत्र items के CPU-heavy processing के लिए शानदार। (केवल वास्तव में CPU-bound, बड़े काम के लिए सार्थक — कुछ overhead है।)
// parallel code accessing SHARED state needs synchronization or thread-safe types
lock (_lock) { _counter++; } // lock for mutual exclusion
Interlocked.Increment(ref _counter); // lock-free atomic
var dict = new ConcurrentDictionary<string, int>(); // thread-safe collection
async/await से परे Tasks और parallelism को समझना performant C# applications बनाने के लिए महत्वपूर्ण senior-level ज्ञान है, और I/O concurrency और CPU parallelism के बीच महत्वपूर्ण अंतर मुख्य अंतर्दृष्टि है — भ्रम का एक सामान्य बिंदु। async/await I/O-bound concurrency संभालता है (प्रतीक्षा के दौरान threads मुक्त करना) लेकिन CPU parallelism नहीं जोड़ता, जबकि CPU-bound काम (computations, data processing) Task.Run (CPU काम को thread pool threads पर offload करना), Parallel.For/ForEach, और PLINQ (.AsParallel()) के माध्यम से cores में वास्तविक parallelism से लाभान्वित होता है।
यह जानना कि कौन सा उपकरण उपयुक्त है — I/O के लिए async/await, CPU-bound काम के लिए TPL parallelism उपकरण — प्रभावी performance optimization के लिए आवश्यक है, क्योंकि गलत का उपयोग करना (जैसे computation को तेज करने के लिए async की अपेक्षा करना, या I/O के लिए threads spin up करना जिसे async कुशलता से संभालता है) अप्रभावी है।
समान रूप से महत्वपूर्ण है thread safety को समझना: shared state तक पहुंचने वाला parallel code race conditions से बचने के लिए synchronization (lock, atomic operations के लिए Interlocked, या thread-safe collections जैसे ConcurrentDictionary) की आवश्यकता रखता है — parallel code में एक महत्वपूर्ण correctness चिंता।
I/O-बनाम-CPU अंतर, parallelism उपकरण (Task.Run, Parallel, PLINQ), और thread-safety आवश्यकताओं को समझना सही, performant concurrent और parallel C# लिखने के लिए मूल्यवान है।
चूंकि आधुनिक applications को अक्सर I/O concurrency और CPU parallelism दोनों की आवश्यकता होती है, और चूंकि सही दृष्टिकोण चुनना और thread safety को सही ढंग से संभालना वही है जो parallel code को प्रभावी और सही बनाता है, यह performance-sensitive C# विकास के लिए महत्वपूर्ण, बारंबार-प्रासंगिक senior ज्ञान है।