线程安全 一

本文通过实例探讨了在C#中使用不同同步机制(上下文绑定、代码区域同步属性)来解决线程安全问题,包括实例方法、静态方法的并发执行与互斥操作,以及如何避免竞态条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Threading;
using System.Runtime.Remoting.Contexts;
using System.Runtime.CompilerServices;

// Note that the instance variable count is shared between the two methods Read
// and Increment. Threads concurrently executing one or both of these methods can
// interfere with each other unless action is taken to synchronize access

class CounterUnsafe
{
    int count = 0;

    public void Increment() 
    {
        // following code is not thread-safe and has a race condition
        Console.WriteLine(
            "Start Resource writing count: {0}", count);
        // the following four lines simulate count++ with a very large
        
// window of time between count being read and being incremented.
        
// This large window ensures that the race condition will create
        
// errors often when the code is accessed concurrently by multiple threads.
        int tempCount = count;
        Thread.Sleep(50);
        tempCount++;
        count = tempCount;
        Console.WriteLine(
            "Stop  Resource writing count: {0}",  count);
    }
}

// Context-bound type with the Synchronization context attribute.
[Synchronization()]
class CounterSynchronizedContext : ContextBoundObject
{
    static int staticCount = 0;
    int instanceCount = 0;

    public void IncrementInstance() 
    {
        Console.WriteLine(
            "Start Object:{0} Thread:{1}  Resource writing count, instance:{2}"
            this.GetHashCode(), Thread.CurrentThread.GetHashCode(),
            instanceCount);
        int tempStaticCount = staticCount;
        int tempInstanceCount = instanceCount;
        Thread.Sleep(50);
        tempInstanceCount++;
        instanceCount = tempInstanceCount;
        Console.WriteLine(
            "Stop Object:{0} Thread:{1}  Resource writing count, instance:{2}"
            this.GetHashCode(), Thread.CurrentThread.GetHashCode(),
            instanceCount);
    }    
    
     public static void IncrementStatic() 
        {
            Console.WriteLine(
                "Start Thread:{0}  Resource writing count, static:{1}"
                Thread.CurrentThread.GetHashCode(),
                staticCount);
            int tempStaticCount = staticCount;
            Thread.Sleep(50);
            tempStaticCount++;
            staticCount = tempStaticCount;
            Console.WriteLine(
                "Stop Thread:{0}  Resource writing count, static:{1}"
                Thread.CurrentThread.GetHashCode(),
                staticCount);
        }
}
class CounterSynchronizedCodeRegion
{
    static int staticCount = 0;
    int instanceCount = 0;

    [MethodImplAttribute(MethodImplOptions.Synchronized)]
    public void IncrementInstance() 
    {
        Console.WriteLine(
            "Start Object:{0} Thread:{1}  Resource writing count, instance:{2}"
            this.GetHashCode(), Thread.CurrentThread.GetHashCode(),
            instanceCount);
        int tempInstanceCount = instanceCount;
        Thread.Sleep(50);
        tempInstanceCount++;
        instanceCount = tempInstanceCount;
        Console.WriteLine(
            "Stop Object:{0} Thread:{1}  Resource writing count, instance:{2}"
            this.GetHashCode(), Thread.CurrentThread.GetHashCode(),
            instanceCount);
    }

    [MethodImplAttribute(MethodImplOptions.Synchronized)]
     public static void IncrementStatic() 
    {
        Console.WriteLine(
        "Start Thread:{0}  Resource writing count, static:{1}"
            Thread.CurrentThread.GetHashCode(),
            staticCount);
        int tempStaticCount = staticCount;
        Thread.Sleep(50);
        tempStaticCount++;
        staticCount = tempStaticCount;
        Console.WriteLine(
            "Stop Thread:{0}  Resource writing count, static:{1}"
            Thread.CurrentThread.GetHashCode(),
            staticCount);
    }
}


class App 
{
    public static void Main() 
    {
        Thread t1, t2, t3;
        Console.WriteLine("\n\nStarting Unsafe Test");
        CounterUnsafe counterUnsafe = new CounterUnsafe();
        t1 = new 
            Thread(    new 
                ThreadStart(counterUnsafe.Increment)); 
        t1.Start();
        t2 = new 
            Thread(    new 
                ThreadStart(counterUnsafe.Increment)); 
        t2.Start();
        t3 = new 
            Thread(    new 
                ThreadStart(counterUnsafe.Increment)); 
        t3.Start();
        // wait for all threads to indicate that they are done.
        t1.Join();t2.Join();t3.Join();
        Console.WriteLine("All Unsafe threads have completed.");
        
        // Synchronized context doesn't prevent static methods from concurrently executing
        Console.WriteLine("\n\nStarting Static Method Synchronized Context Test");
        t1 = new 
            Thread(    new 
            ThreadStart(CounterSynchronizedContext.IncrementStatic)); 
        t1.Start();
        t2 = new 
            Thread(    new 
            ThreadStart(CounterSynchronizedContext.IncrementStatic)); 
        t2.Start();
        t3 = new 
            Thread(    new 
            ThreadStart(CounterSynchronizedContext.IncrementStatic)); 
        t3.Start();

        // wait for all threads to indicate that they are done.
        t1.Join();t2.Join();t3.Join();
        Console.WriteLine("All Static Method Synchronized Context threads have completed.");
        
        // Synchronized context does prevent instance methods from concurrently executing
        Console.WriteLine("\n\nStarting Instance Method Synchronized Context Test");
        CounterSynchronizedContext counterSynchronizedContext = new CounterSynchronizedContext();
        t1 = new 
            Thread(    new 
            ThreadStart(counterSynchronizedContext.IncrementInstance)); 
        t1.Start();
        t2 = new 
            Thread(    new 
            ThreadStart(counterSynchronizedContext.IncrementInstance)); 
        t2.Start();
        t3 = new 
            Thread(    new 
            ThreadStart(counterSynchronizedContext.IncrementInstance)); 
        t3.Start();

        // wait for all threads to indicate that they are done.
        t1.Join();t2.Join();t3.Join();
        Console.WriteLine("All Instance Method Synchronized Context threads have completed.");
        
        Console.WriteLine("\n\nStarting Static Method Synchronized Code Region Test");
        t1 = new 
            Thread(    new 
            ThreadStart(CounterSynchronizedCodeRegion.IncrementStatic)); 
        t1.Start();
        t2 = new 
            Thread(    new 
            ThreadStart(CounterSynchronizedCodeRegion.IncrementStatic)); 
        t2.Start();
        t3 = new 
            Thread(    new 
            ThreadStart(CounterSynchronizedCodeRegion.IncrementStatic)); 
        t3.Start();
        // wait for all threads to indicate that they are done.
        t1.Join();t2.Join();t3.Join();

        Console.WriteLine("All Static Method Synchronized Code Region threads have completed.");
        
        // Method Synchronization is similar to Monitor and prevents 
        
// concurrent access to both static and instance methods
        Console.WriteLine("\n\nStarting Instance Method Synchronized Code Region Test");
        CounterSynchronizedCodeRegion counterSynchronizedCodeRegion = new CounterSynchronizedCodeRegion();
        t1 = new 
            Thread(    new 
            ThreadStart(counterSynchronizedCodeRegion.IncrementInstance)); 
        t1.Start();
        t2 = new 
            Thread(    new 
            ThreadStart(counterSynchronizedCodeRegion.IncrementInstance)); 
        t2.Start();
        t3 = new 
            Thread(    new 
            ThreadStart(counterSynchronizedCodeRegion.IncrementInstance)); 
        t3.Start();

        // wait for all threads to indicate that they are done.
        t1.Join();t2.Join();t3.Join();
        Console.WriteLine("All Static Method Synchronized Code Region threads have completed.");

    }


}

转载于:https://www.cnblogs.com/imxh/archive/2011/11/15/2249524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值