C#中的checked、unchecked、lock操作符

checked和unchecked操作符用于整型算术运算时控制当前环境中的溢出检查。下列运算参与了checked和unchecked检查(操作数均为整数):

1)  预定义的++和――一元运算符。

2)  预定义的-一元运算符。

3)  预定义的+、-、×、/等二元操作符。

4)  从一种整型到另一种整型的显示数据转换。


当上述整型运算产生一个目标类型无法表示的大数时,可以有相应的处理方式:


(一)使用checked

若运算是常量表达式,则产生编译错误:The operation overflows at complie time in checked mode.

若运算是非常量表达式,则运行时会抛出一个溢出异常:OverFlowException异常


(二)使用unchecked

无论运算是否是常量表达式,都没有编译错误或是运行时异常发生,只是返回值被截掉不符合目标类型的高位。


(三)既未使用checked又未使用unchecked

若运算是常量表达式,默认情况下总是进行溢出检查,同使用checked一样,会无法通过编译。

若运算是非常量表达式,则是否进行溢出检查,取决于外部因素,包括编译器状态、执行环境参数等。


下例说明了checked和unchecked操作符的使用方法:

class Test

{

       static int x = 1000000;

       static int y = 1000000;

       static int F()

{

       return checked(x*y);     //运行时抛出OverFlowException异常

}

static int G()

{

       return unchecked(x*y);  //截去高位部分,返回-727379968

}

static int H()

{

       return x*y;     //依赖于编译器的默认设置,一般是不检查

}

}

========================================================================

lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁。此语句的形式如下:

Object thisLock = new Object();
lock (thisLock)
{
    // Critical code section
}
//在 C# 中使用线程的简单示例。
// statements_lock.cs
using System;
using System.Threading;

class ThreadTest
{
    public void RunMe()
    {
        Console.WriteLine("RunMe called");
    }

    static void Main()
    {
        ThreadTest b = new ThreadTest();
        Thread t = new Thread(b.RunMe);
        t.Start();
    }
}
 
 

//使用线程和 lock。只要 lock 语句存在,语句块就是临界区并且 balance 永远不会是负数。

// statements_lock2.cs
using System;
using System.Threading;

class Account
{
    private Object thisLock = new Object();
    int balance;

    Random r = new Random();

    public Account(int initial)
    {
        balance = initial;
    }

    int Withdraw(int amount)
    {

        // This condition will never be true unless the lock statement
        // is commented out:
        if (balance < 0)
        {
            throw new Exception("Negative Balance");
        }

        // Comment out the next line to see the effect of leaving out 
        // the lock keyword:
        lock(thisLock)
        {
            if (balance >= amount)
            {
                Console.WriteLine("Balance before Withdrawal :  " + balance);
                Console.WriteLine("Amount to Withdraw        : -" + amount);
                balance = balance - amount;
                Console.WriteLine("Balance after Withdrawal  :  " + balance);
                return amount;
            }
            else
            {
                return 0; // transaction rejected
            }
        }
    }

    public void DoTransactions()
    {
        for (int i = 0; i < 100; i++)
        {
            Withdraw(r.Next(1, 100));
        }
    }
}

class Test
{
    static void Main()
    {
        Thread[] threads = new Thread[10];
        Account acc = new Account(1000);
        for (int i = 0; i < 10; i++)
        {
            Thread t = new Thread(new ThreadStart(acc.DoTransactions));
            threads[i] = t;
        }
        for (int i = 0; i < 10; i++)
        {
            threads[i].Start();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值