次の方法で共有


Queue.IsSynchronized プロパティ

Queue へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

Public Overridable ReadOnly Property IsSynchronized As Boolean  _   Implements ICollection.IsSynchronized
[C#]
public virtual bool IsSynchronized {get;}
[C++]
public: __property virtual bool get_IsSynchronized();
[JScript]
public function get IsSynchronized() : Boolean;

プロパティ値

Queue へのアクセスが同期されている (スレッド セーフである) 場合は true 。それ以外の場合は false 。既定値は false です。

実装

ICollection.IsSynchronized

解説

Queue を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。

コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチする方法のいずれかを実行できます。

[Visual Basic, C#] 列挙処理中に SyncRoot を使用してコレクションをロックする方法を次のコード例に示します。

 
Queue myCollection = new Queue();
 lock( myCollection.SyncRoot ) {
 foreach ( Object item in myCollection ) {
 // Insert your code here.
 }
}
[Visual Basic] 
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

使用例

[Visual Basic, C#, C++] 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. 

[C#] 
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.
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

int main()  {

   // Creates and initializes a new Queue.
   Queue* myQ = new Queue();
   myQ->Enqueue( S"The" );
   myQ->Enqueue( S"quick" );
   myQ->Enqueue( S"brown" );
   myQ->Enqueue( S"fox" );

   // Creates a synchronized wrapper around the Queue.
   Queue* mySyncdQ = Queue::Synchronized( myQ );

   // Displays the sychronization status of both Queues.
   Console::WriteLine( S"myQ is {0}.", myQ->IsSynchronized ? S"synchronized" : S"not synchronized" );
   Console::WriteLine( S"mySyncdQ is {0}.", mySyncdQ->IsSynchronized ? S"synchronized" : S"not synchronized" );
}

/*
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Queue クラス | Queue メンバ | System.Collections 名前空間 | SyncRoot | Synchronized