IProducerConsumerCollection<T> 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義方法,以操作用於產生者/取用者使用的安全線程集合。 這個介面提供生產者/取用者集合的統一表示法,讓較高層級的抽象概念,例如 BlockingCollection<T> 可以使用集合作為基礎儲存機制。
generic <typename T>
public interface class IProducerConsumerCollection : System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection
public interface IProducerConsumerCollection<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
type IProducerConsumerCollection<'T> = interface
interface seq<'T>
interface IEnumerable
interface ICollection
type IProducerConsumerCollection<'T> = interface
interface seq<'T>
interface ICollection
interface IEnumerable
Public Interface IProducerConsumerCollection(Of T)
Implements ICollection, IEnumerable(Of T)
類型參數
- T
指定集合中的項目類型。
- 衍生
- 實作
範例
下列範例顯示實作 System.Collections.Concurrent.IProducerConsumerCollection<T>的堆疊數據結構。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
// Sample implementation of IProducerConsumerCollection(T)
// -- in this case, a thread-safe stack.
public class SafeStack<T> : IProducerConsumerCollection<T>
{
// Used for enforcing thread-safety
private object m_lockObject = new object();
// We'll use a regular old Stack for our core operations
private Stack<T> m_sequentialStack = null;
//
// Constructors
//
public SafeStack()
{
m_sequentialStack = new Stack<T>();
}
public SafeStack(IEnumerable<T> collection)
{
m_sequentialStack = new Stack<T>(collection);
}
//
// Safe Push/Pop support
//
public void Push(T item)
{
lock (m_lockObject) m_sequentialStack.Push(item);
}
public bool TryPop(out T item)
{
bool rval = true;
lock (m_lockObject)
{
if (m_sequentialStack.Count == 0) { item = default(T); rval = false; }
else
{
item = m_sequentialStack.Pop();
}
}
return rval;
}
//
// IProducerConsumerCollection(T) support
//
public bool TryTake(out T item)
{
return TryPop(out item);
}
public bool TryAdd(T item)
{
Push(item);
return true; // Push doesn't fail
}
public T[] ToArray()
{
T[] rval = null;
lock (m_lockObject) rval = m_sequentialStack.ToArray();
return rval;
}
public void CopyTo(T[] array, int index)
{
lock (m_lockObject) m_sequentialStack.CopyTo(array, index);
}
//
// Support for IEnumerable(T)
//
public IEnumerator<T> GetEnumerator()
{
// The performance here will be unfortunate for large stacks,
// but thread-safety is effectively implemented.
Stack<T> stackCopy = null;
lock (m_lockObject) stackCopy = new Stack<T>(m_sequentialStack);
return stackCopy.GetEnumerator();
}
//
// Support for IEnumerable
//
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
//
// Support for ICollection
//
public bool IsSynchronized
{
get { return true; }
}
public object SyncRoot
{
get { return m_lockObject; }
}
public int Count
{
get { return m_sequentialStack.Count; }
}
public void CopyTo(Array array, int index)
{
lock (m_lockObject) ((ICollection)m_sequentialStack).CopyTo(array, index);
}
}
public class Program
{
static void Main()
{
TestSafeStack();
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
// Test our implementation of IProducerConsumerCollection(T)
// Demonstrates:
// IPCC(T).TryAdd()
// IPCC(T).TryTake()
// IPCC(T).CopyTo()
static void TestSafeStack()
{
SafeStack<int> stack = new SafeStack<int>();
IProducerConsumerCollection<int> ipcc = (IProducerConsumerCollection<int>)stack;
// Test Push()/TryAdd()
stack.Push(10); Console.WriteLine("Pushed 10");
ipcc.TryAdd(20); Console.WriteLine("IPCC.TryAdded 20");
stack.Push(15); Console.WriteLine("Pushed 15");
int[] testArray = new int[3];
// Try CopyTo() within boundaries
try
{
ipcc.CopyTo(testArray, 0);
Console.WriteLine("CopyTo() within boundaries worked, as expected");
}
catch (Exception e)
{
Console.WriteLine("CopyTo() within boundaries unexpectedly threw an exception: {0}", e.Message);
}
// Try CopyTo() that overflows
try
{
ipcc.CopyTo(testArray, 1);
Console.WriteLine("CopyTo() with index overflow worked, and it SHOULD NOT HAVE");
}
catch (Exception e)
{
Console.WriteLine("CopyTo() with index overflow threw an exception, as expected: {0}", e.Message);
}
// Test enumeration
Console.Write("Enumeration (should be three items): ");
foreach (int item in stack) Console.Write("{0} ", item);
Console.WriteLine("");
// Test TryPop()
int popped = 0;
if (stack.TryPop(out popped))
{
Console.WriteLine("Successfully popped {0}", popped);
}
else
{
Console.WriteLine("FAILED to pop!!");
}
// Test Count
Console.WriteLine("stack count is {0}, should be 2", stack.Count);
// Test TryTake()
if (ipcc.TryTake(out popped))
{
Console.WriteLine("Successfully IPCC-TryTaked {0}", popped);
}
else
{
Console.WriteLine("FAILED to IPCC.TryTake!!");
}
}
}
Imports System.Collections.Concurrent
Module IProdCon
' Sample implementation of IProducerConsumerCollection(T) -- in this case,
' a thread-safe stack.
Public Class SafeStack(Of T)
Implements IProducerConsumerCollection(Of T)
' Used for enforcing thread-safety
Private m_lockObject As New Object()
' We'll use a regular old Stack for our core operations
Private m_sequentialStack As Stack(Of T) = Nothing
'
' Constructors
'
Public Sub New()
m_sequentialStack = New Stack(Of T)()
End Sub
Public Sub New(ByVal collection As IEnumerable(Of T))
m_sequentialStack = New Stack(Of T)(collection)
End Sub
'
' Safe Push/Pop support
'
Public Sub Push(ByVal item As T)
SyncLock m_lockObject
m_sequentialStack.Push(item)
End SyncLock
End Sub
Public Function TryPop(ByRef item As T) As Boolean
Dim rval As Boolean = True
SyncLock m_lockObject
If m_sequentialStack.Count = 0 Then
item = Nothing
rval = False
Else
item = m_sequentialStack.Pop()
End If
End SyncLock
Return rval
End Function
'
' IProducerConsumerCollection(T) support
'
Public Function TryTake(ByRef item As T) As Boolean Implements IProducerConsumerCollection(Of T).TryTake
Return TryPop(item)
End Function
Public Function TryAdd(ByVal item As T) As Boolean Implements IProducerConsumerCollection(Of T).TryAdd
Push(item)
' Push doesn't fail
Return True
End Function
Public Function ToArray() As T() Implements IProducerConsumerCollection(Of T).ToArray
Dim rval As T() = Nothing
SyncLock m_lockObject
rval = m_sequentialStack.ToArray()
End SyncLock
Return rval
End Function
Public Sub CopyTo(ByVal array As T(), ByVal index As Integer) Implements IProducerConsumerCollection(Of T).CopyTo
SyncLock m_lockObject
m_sequentialStack.CopyTo(array, index)
End SyncLock
End Sub
'
' Support for IEnumerable(T)
'
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
' The performance here will be unfortunate for large stacks,
' but thread-safety is effectively implemented.
Dim stackCopy As Stack(Of T) = Nothing
SyncLock m_lockObject
stackCopy = New Stack(Of T)(m_sequentialStack)
End SyncLock
Return stackCopy.GetEnumerator()
End Function
'
' Support for IEnumerable
'
Private Function GetEnumerator2() As IEnumerator Implements IEnumerable.GetEnumerator
Return DirectCast(Me, IEnumerable(Of T)).GetEnumerator()
End Function
'
' Support for ICollection
'
Public ReadOnly Property IsSynchronized() As Boolean Implements ICollection.IsSynchronized
Get
Return True
End Get
End Property
Public ReadOnly Property SyncRoot() As Object Implements ICollection.SyncRoot
Get
Return m_lockObject
End Get
End Property
Public ReadOnly Property Count() As Integer Implements ICollection.Count
Get
Return m_sequentialStack.Count
End Get
End Property
Public Sub CopyTo(ByVal array As Array, ByVal index As Integer) Implements ICollection.CopyTo
SyncLock m_lockObject
DirectCast(m_sequentialStack, ICollection).CopyTo(array, index)
End SyncLock
End Sub
End Class
' Test our implementation of IProducerConsumerCollection(T)
' Demonstrates:
' IPCC(T).TryAdd()
' IPCC(T).TryTake()
' IPCC(T).CopyTo()
Private Sub TestSafeStack()
Dim stack As New SafeStack(Of Integer)()
Dim ipcc As IProducerConsumerCollection(Of Integer) = DirectCast(stack, IProducerConsumerCollection(Of Integer))
' Test Push()/TryAdd()
stack.Push(10)
Console.WriteLine("Pushed 10")
ipcc.TryAdd(20)
Console.WriteLine("IPCC.TryAdded 20")
stack.Push(15)
Console.WriteLine("Pushed 15")
Dim testArray As Integer() = New Integer(2) {}
' Try CopyTo() within boundaries
Try
ipcc.CopyTo(testArray, 0)
Console.WriteLine("CopyTo() within boundaries worked, as expected")
Catch e As Exception
Console.WriteLine("CopyTo() within boundaries unexpectedly threw an exception: {0}", e.Message)
End Try
' Try CopyTo() that overflows
Try
ipcc.CopyTo(testArray, 1)
Console.WriteLine("CopyTo() with index overflow worked, and it SHOULD NOT HAVE")
Catch e As Exception
Console.WriteLine("CopyTo() with index overflow threw an exception, as expected: {0}", e.Message)
End Try
' Test enumeration
Console.Write("Enumeration (should be three items): ")
For Each item As Integer In stack
Console.Write("{0} ", item)
Next
Console.WriteLine("")
' Test TryPop()
Dim popped As Integer = 0
If stack.TryPop(popped) Then
Console.WriteLine("Successfully popped {0}", popped)
Else
Console.WriteLine("FAILED to pop!!")
End If
' Test Count
Console.WriteLine("stack count is {0}, should be 2", stack.Count)
' Test TryTake()
If ipcc.TryTake(popped) Then
Console.WriteLine("Successfully IPCC-TryTaked {0}", popped)
Else
Console.WriteLine("FAILED to IPCC.TryTake!!")
End If
End Sub
Sub Main()
TestSafeStack()
' Keep the console window open in debug mode
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
End Module
備註
如需詳細資訊,請參閱 Thread-Safe 集合 和 BlockingCollection 概觀。
屬性
Count |
取得包含在 ICollection中的項目數目。 (繼承來源 ICollection) |
IsSynchronized |
取得值,指出是否同步存取 ICollection (線程安全)。 (繼承來源 ICollection) |
SyncRoot |
取得對象,這個物件可用來同步存取 ICollection。 (繼承來源 ICollection) |
方法
CopyTo(Array, Int32) |
從特定 Array 索引開始,將 ICollection 的專案複製到 Array。 (繼承來源 ICollection) |
CopyTo(T[], Int32) |
從指定的索引開始,將 IProducerConsumerCollection<T> 的專案複製到 Array。 |
GetEnumerator() |
傳回逐一查看集合的列舉值。 (繼承來源 IEnumerable) |
ToArray() |
將包含在 IProducerConsumerCollection<T> 中的項目複製到新的數位。 |
TryAdd(T) |
試著將 物件加入至 IProducerConsumerCollection<T>。 |
TryTake(T) |
嘗試從 IProducerConsumerCollection<T>移除並傳回 物件。 |
擴充方法
適用於
執行緒安全性
此介面的所有實作都必須讓這個介面的所有成員同時從多個線程使用。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應