BlockingCollection blockingCollection = new BlockingCollection();
// create and start a producer
Task.Factory.StartNew(() => {
// put items into the collectioon
for (int i = 0; i < 1000; i++)
{
blockingCollection.Add(i);
}
// mark the collection as complete
blockingCollection.CompleteAdding();
});
// create and start a consumer
Task.Factory.StartNew(() => {
while (!blockingCollection.IsCompleted)
{
// take an item from the collection
int item = blockingCollection.Take();
// print out the item
Console.WriteLine("Taskid{1} Item {0}", item, Task.CurrentId);
}
});
// create and start a consumer
Task.Factory.StartNew(() => {
while (!blockingCollection.IsCompleted) // if the blockingCollection is not completed
{
// take an item from the collection
int item = blockingCollection.Take(); // but in here, all the items have been taken by other thread, this line will wait forever?
// print out the item
Console.WriteLine("Taskid{1} Item {0}", item,Task.CurrentId);
}
});
Console.ReadLine();
Ключевые строки кода:
while (!blockingCollection.IsCompleted) // if the blockingCollection is not completed
int item = blockingCollection.Take(); // but in here, all the items have been taken by other thread, this line will wait forever?
Подробнее здесь: https://stackoverflow.com/questions/597 ... hread-safe
Является ли BlockingCollection потокобезопасным? ⇐ C#
Место общения программистов C#
-
Anonymous
1732569103
Anonymous
BlockingCollection blockingCollection = new BlockingCollection();
// create and start a producer
Task.Factory.StartNew(() => {
// put items into the collectioon
for (int i = 0; i < 1000; i++)
{
blockingCollection.Add(i);
}
// mark the collection as complete
blockingCollection.CompleteAdding();
});
// create and start a consumer
Task.Factory.StartNew(() => {
while (!blockingCollection.IsCompleted)
{
// take an item from the collection
int item = blockingCollection.Take();
// print out the item
Console.WriteLine("Taskid{1} Item {0}", item, Task.CurrentId);
}
});
// create and start a consumer
Task.Factory.StartNew(() => {
while (!blockingCollection.IsCompleted) // if the blockingCollection is not completed
{
// take an item from the collection
int item = blockingCollection.Take(); // but in here, all the items have been taken by other thread, this line will wait forever?
// print out the item
Console.WriteLine("Taskid{1} Item {0}", item,Task.CurrentId);
}
});
Console.ReadLine();
Ключевые строки кода:
while (!blockingCollection.IsCompleted) // if the blockingCollection is not completed
int item = blockingCollection.Take(); // but in here, all the items have been taken by other thread, this line will wait forever?
Подробнее здесь: [url]https://stackoverflow.com/questions/59728872/is-the-blockingcollection-thread-safe[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия