I/O साठी / च्या पलीकडे, .NET प्रदान करतो साठी — गणना अनेक cores मध्ये चालवण्यासाठी. मुख्य फरक: / हे I/O concurrency साठी आहे (non-blocking waits), तर , , आणि PLINQ हे CPU-intensive कामाला parallelize करण्यासाठी आहेत.
I/O साठी / च्या पलीकडे, .NET प्रदान करतो साठी — गणना अनेक cores मध्ये चालवण्यासाठी. मुख्य फरक: / हे 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 work ला 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 मध्ये काम वितरित करतात — CPU-heavy processing साठी स्वतंत्र items साठी उत्तम. (केवळ खरोखर 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
Tasks आणि parallelism समजणे async/await च्या पलीकडे हे senior-level ज्ञान महत्वाचे आहे performant C# applications बनवण्यासाठी, आणि I/O concurrency आणि CPU parallelism मधील गंभीर फरक हा मुख्य अंतर्दृष्टी आहे — एक सामान्य गोंधळाचा बिंदू. async/await I/O-bound concurrency हाताळतो (waits दरम्यान threads मुक्त करून) परंतु CPU parallelism जोडत नाही, तर CPU-bound कामाला (computations, data processing) cores मध्ये वास्तविक parallelism मिळते Task.Run द्वारे (CPU कामाला thread pool threads मध्ये transfer करून), Parallel.For/ForEach, आणि PLINQ (.AsParallel()) द्वारे.
कोणते tool योग्य आहे हे जाणणे — async/await I/O साठी, TPL parallelism tools CPU-bound कामासाठी — हे कार्यक्षम performance optimization साठी आवश्यक आहे, कारण चुकीचे tool वापरणे (उदा. async ला computation गतिवान करण्याची अपेक्षा करणे, किंवा I/O साठी threads तयार करणे जे async efficiently हाताळते) अप्रभावी आहे.
समान महत्वाचे आहे thread safety समजणे: parallel code shared state access करतो ज्याला synchronization (lock, atomic operations साठी Interlocked, किंवा thread-safe collections जसे ConcurrentDictionary) आवश्यक आहे race conditions टाळण्यासाठी — parallel code मधील गंभीर correctness चिंता.
I/O-vs-CPU फरक, parallelism tools (Task.Run, Parallel, PLINQ), आणि thread-safety आवश्यकता समजणे हे valuable आहे सही, performant concurrent आणि parallel C# लिहिण्यासाठी.
आधुनिक applications बहुतेक वेळा I/O concurrency आणि CPU parallelism दोन्ही गरजेत आहेत, आणि सही approach निवडणे आणि thread safety सही हाताळणे हे जे parallel code effective आणि सही करते, हे महत्वाचे, वारंवार-संबंधित senior knowledge आहे performance-sensitive C# development साठी.
सविस्तर उत्तरांसह IT मुलाखत प्रश्नांचे ग्रंथालय — Junior पासून Senior पर्यंत.
देणगी द्या