2024年1月28日发(作者:)

static void Main(string[] args) { new Thread(Read){ IsBackground = true }.Start(); new Thread(Read){ IsBackground = true }.Start(); new Thread(Read){ IsBackground = true }.Start(); new Thread(() => Write("Thread 1")){ IsBackground = true }.Start(); new Thread(() => Write("Thread 2")){ IsBackground = true }.Start(); Sleep(conds(30)); } static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim(); static Dictionary _items = new Dictionary(); static void Read() { WriteLine("Reading contents of a dictionary"); while (true) { try { _eadLock(); foreach (var key in _) { Sleep(conds(0.1)); } } finally { _adLock(); } } } static void Write(string threadName) { while (true) { try { int newKey = new Random().Next(250); _pgradeableReadLock(); if (!_nsKey(newKey)) { try { _riteLock(); _items[newKey] = 1; WriteLine($"New key {newKey} is added to a dictionary by a {threadName}"); } finally { _iteLock(); } } Sleep(conds(0.1)); } finally { _gradeableReadLock(); } } }

调整plinq查询的参数try { parallelQuery .WithDegreeOfParallelism(sorCount) .WithExecutionMode(arallelism) .WithMergeOptions(t) .WithCancellation() .ForAll(WriteLine); }处理plinq查询中的异常 try { (WriteLine); } catch (DivideByZeroException) { WriteLine("Divided by zero - usual exception handler!"); } catch (AggregateException e) { n().Handle(ex => { if (ex is DivideByZeroException) { WriteLine("Divided by zero - aggregate exception handler!"); return true; }

return false; }); }管理plinq查询中的数据分区