public BlockingCollection(int boundedCapacity)
public BlockingCollection(IProducerConsumerCollection<T> collection) public BlockingCollection(IProducerConsumerCollection<T> collection,
int boundedCapacity)
BlockingCollection<T> , ConcurrentQueue<T>. , BlockingCollection<T>. boundedCapaci ty, , , . boundedCapaci ty , .
TryAdd () TryTake (), , IProducerConsumerCollection<T>, BlockingCollection<T> . , .
public void Add(T item) public T Take()
Add () , item . Add () , , . , item , . () . ( , CancellationToken.)
Add () (), "-", . , Z, , . BlockingCollection<T>, 4 .
// BlockingCollection. using System;
Using System.Threading.Tasks;
Using System.Threading;
Using System.Collections.Concurrent;
class BlockingDemo {
static BlockingCollection<char> be;
// Z. static void Producer () {
for(char ch = 'A'; ch <= 'Z'; ch++) { be.Add(ch);
Console.WriteLine (" " + ch);
}
}
// 26 , static void Consumer() {
for(int i=0; i < 26; i++)
Console.WriteLine (" " + bc.TakeO);
}
static void Main() {
// , 4 , be = new BlockingCollection<char>(4);
// .
Task Prod = new Task(Producer);
Task Con = new Task(Consumer);
|
|
// .
Con.Start ();
Prod:Start();
// , try {
Task.WaitAll(Con, Prod);
} catch(AggregateException exc) {
Console.WriteLine (exc);
} finally {
Con.Dispose ();
Prod.Dispose (); be.Dispose();
}
}
}
, , . , 4 , , , . . , , - . , . .
BlockingCollection<T> CompleteAdding (). .