Queue.Synchronized メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim queue As Queue Dim returnValue As Queue returnValue = Queue.Synchronized(queue)
戻り値
同期されている (スレッド セーフな) Queue ラッパー。


![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization です。HostProtectionAttribute は、デスクトップ アプリケーション (通常、アイコンのダブルクリック、コマンドの入力、またはブラウザへの URL の入力により起動されるもの) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
Queue を確実にスレッド セーフにするためには、すべての操作をこのラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

列挙処理中に SyncRoot を使用してコレクションをロックする方法を次のコード例に示します。このメソッドは O(1) 操作です。
Queue myCollection = new Queue(); lock(myCollection.SyncRoot) { foreach (Object item in myCollection) { // Insert your code here. } }
Dim myCollection As New Queue() Dim item As Object SyncLock myCollection.SyncRoot For Each item In myCollection ' Insert your code here. Next item End SyncLock
Queue を同期する方法と、Queue が同期されているかどうかを確認し、同期済みの Queue を使用する方法の例を次に示します。
Imports System Imports System.Collections Public Class SamplesQueue Public Shared Sub Main() ' Creates and initializes a new Queue. Dim myQ As New Queue() myQ.Enqueue("The") myQ.Enqueue("quick") myQ.Enqueue("brown") myQ.Enqueue("fox") ' Creates a synchronized wrapper around the Queue. Dim mySyncdQ As Queue = Queue.Synchronized(myQ) ' Displays the sychronization status of both Queues. Dim msg As String If myQ.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("myQ is {0}.", msg) If mySyncdQ.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("mySyncdQ is {0}.", msg) End Sub End Class ' This code produces the following output. ' ' myQ is not synchronized. ' mySyncdQ is synchronized.
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); // Creates a synchronized wrapper around the Queue. Queue mySyncdQ = Queue.Synchronized( myQ ); // Displays the sychronization status of both Queues. Console.WriteLine( "myQ is {0}.", myQ.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdQ is {0}.", mySyncdQ.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myQ is not synchronized. mySyncdQ is synchronized. */
#using <system.dll> using namespace System; using namespace System::Collections; int main() { // Creates and initializes a new Queue. Queue^ myQ = gcnew Queue; myQ->Enqueue( "The" ); myQ->Enqueue( "quick" ); myQ->Enqueue( "brown" ); myQ->Enqueue( "fox" ); // Creates a synchronized wrapper around the Queue. Queue^ mySyncdQ = Queue::Synchronized( myQ ); // Displays the sychronization status of both Queues. Console::WriteLine( "myQ is {0}.", myQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" ); Console::WriteLine( "mySyncdQ is {0}.", mySyncdQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" ); } /* This code produces the following output. myQ is not synchronized. mySyncdQ is synchronized. */
import System.*; import System.Collections.*; public class SamplesQueue { public static void main(String[] args) { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue("The"); myQ.Enqueue("quick"); myQ.Enqueue("brown"); myQ.Enqueue("fox"); // Creates a synchronized wrapper around the Queue. Queue mySyncdQ = Queue.Synchronized(myQ); // Displays the sychronization status of both Queues. Console.WriteLine("myQ is {0}.", (myQ.get_IsSynchronized()) ? "synchronized" : "not synchronized"); Console.WriteLine("mySyncdQ is {0}.",(mySyncdQ.get_IsSynchronized()) ? "synchronized" : "not synchronized"); } //main } //SamplesQueue /* This code produces the following output. myQ is not synchronized. mySyncdQ is synchronized. */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からQueue.Synchronized メソッドを検索する場合は、下記のリンクをクリックしてください。

- Queue.Synchronized メソッドのページへのリンク