Array.SyncRoot Property
2007-11-08 16:38
239 查看
Syntax:
Example:
Accesses to arrays are easily synchronized with the lock statement, where the SyncRoot object is the parameter.
using System;
using System.Collections.Generic;
using System.Threading;
public class MyClass
{
public static void Main()
{
Array.Sort(zArray);
Thread t1=new Thread(
new ThreadStart(DisplayForward));
Thread t2=new Thread(
new ThreadStart(DisplayReverse));
t1.Start();
t2.Start();
RL();
}
private static int [] zArray={1,5,4,2,4,2,9,10};
public static void DisplayForward() {
lock(zArray.SyncRoot) {
Console.Write(" Forward: ");
foreach(int number in zArray) {
Console.Write(number+" ");
}
}
}
public static void DisplayReverse() {
lock(zArray.SyncRoot) {
Array.Reverse(zArray);
Console.Write(" Reverse: ");
foreach(int number in zArray) {
Console.Write(number+" ");
}
Array.Reverse(zArray);
}
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
o(∩_∩)o...
SyncRoot This property returns a synchronization object for the current array. Arrays are not inherently thread-safe. Synchronize access to the array with the synchronization object. | virtual object SyncRoot{ get;} |
Accesses to arrays are easily synchronized with the lock statement, where the SyncRoot object is the parameter.
using System;
using System.Collections.Generic;
using System.Threading;
public class MyClass
{
public static void Main()
{
Array.Sort(zArray);
Thread t1=new Thread(
new ThreadStart(DisplayForward));
Thread t2=new Thread(
new ThreadStart(DisplayReverse));
t1.Start();
t2.Start();
RL();
}
private static int [] zArray={1,5,4,2,4,2,9,10};
public static void DisplayForward() {
lock(zArray.SyncRoot) {
Console.Write(" Forward: ");
foreach(int number in zArray) {
Console.Write(number+" ");
}
}
}
public static void DisplayReverse() {
lock(zArray.SyncRoot) {
Array.Reverse(zArray);
Console.Write(" Reverse: ");
foreach(int number in zArray) {
Console.Write(number+" ");
}
Array.Reverse(zArray);
}
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
o(∩_∩)o...
相关文章推荐
- java.lang.IllegalStateException: Web app root system property already set to different value: 'webap
- Web app root system property already set to different value
- 部署两个项目时报 Web app root system property already set to different value:
- Synchronized vs SyncRoot
- Synchronized vs SyncRoot
- java.lang.IllegalStateException: Web app root system property already set to different value
- Web app root system property already set to different value: 'webapp.root'
- 同一个tomcat APP中的 Log4j 的App root System property 不能相同,需要设置不同,不然会出现以下错误。解决方法在错误中标明了。
- Tomcat 7下SSL配置出现异常: '/root/.keystore' did not find a matching property
- JS Caffeine Script & Add property to every object in array & Counting power sets&Singleton Patter
- Web app root system property already set to different value 的解决办法
- Array.forEach() & Using square brackets to obtain the value of a property
- Flex4之元数据标签:[ArrayElementType] [Bindable] [DefaultProperty] [Deprecated] [Embed] 等
- Synchronized vs SyncRoot
- Web app root system property already set to different value 的解决办法
- C# Hashtable Synchronized vs SyncRoot
- Web app root system property already set to different value
- Web app root system property already set to different value 错误原因及解决
- 用Array做Property会有性能问题
- Unauthe nticated r sync access to Remote Root Code Execution