当前位置:网站首页>C# Monitor class
C# Monitor class
2022-08-02 23:37:00 【biyusr】
lock statement is parsed by the C# compiler to use the Monitor class.The following lock statement:
lock(obj){// synchronized region for obj}
is resolved to call the Enter() method, which waits until the thread locks the object.Only one thread can lock an object at a time, as long as the lock is released.The thread can then enter the synchronization phase.The Exit() method of the Monitor class releases the lock.The compiler puts the Exit() method in the finally handler of the try block, so if an exception is thrown, the lock is released.
Monitor.Enter(obj);try{// synchronized region for obj}finally{Monitor.Exit(obj);}
The main advantage of the Monitor class over C#'s lock statement is that you can add a timeout value for waiting to be locked.This way, instead of waiting indefinitely to be locked, the TryEnter() method can be used as in the example below, passing it a timeout value specifying the maximum time to wait to be locked.If obj is locked, the TryEnter() method sets the Boolean reference parameter to true and synchronously accesses the state locked by the object obj.If another thread locks obj for more than 500 milliseconds, the TryEnter() method sets the variable lockTaken to false, and the thread no longer waits, but is used to perform other operations.Maybe later, the thread will try to acquire the lock again.
bool _lockTaken = false;Monitor.TryEnter(_obj, 500, ref _lockTaken);if (_lockTaken){try{// acquired the lock// synchronized region for obj}finallycode>{Monitor.Exit(obj);}}else{// didn't get the lock, do something else}
边栏推荐
- OP-5,输入/输出信号范围-一信号处理能力
- Packages and packages, access modifiers
- 线程安全(上)
- WPF development through practical 】 【 automatic production management platform
- 即时通讯开发移动端网络短连接的优化手段
- "A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
- ALV report learning summary
- js Fetch返回数据res.json()报错问题
- 广东省数字经济发展指引 1.0之建成数据安全保障体系
- 顺序查找和折半查找,看这篇就够了
猜你喜欢

callback prototype __proto__

SQL Server安装教程

李沐动手学深度学习V2-bert和代码实现

Flink Yarn Per Job - 启动AM

ECCV 2022 | 通往数据高效的Transformer目标检测器

Parse common methods in the Collection interface that are overridden by subclasses

信息学奥赛一本通(1258:【例9.2】数字金字塔)

软件测试的流程规范有哪些?具体要怎么做?

Packages and packages, access modifiers

"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
随机推荐
Axure9的元件用法
Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)
信息学奥赛一本通(1260:【例9.4】拦截导弹(Noip1999))
「 每日一练,快乐水题 」1374. 生成每种字符都是奇数个的字符串
setup syntax sugar defineProps defineEmits defineExpose
J9 Digital Currency Theory: Identifying Web3's New Scarcity: Open Source Developers
第一次进入前20名
The five classification of software testing
How the sensor works
PLC working principle animation
开关、电机、断路器、电热偶、电表接线图大全
二丙二醇甲醚醋酸酯
2170. 使数组变成交替数组的最少操作数
顺序查找和折半查找,看这篇就够了
Digital twins help visualize the construction of smart cities
你是几星测试/开发程序员?技术型选手王大拿......
STP生成树协议
.NET performance optimization - you should set initial size for collection types
如何使用windbg查看C#某个线程的栈大小 ?
OP analysis and design