标签:util div 文章 instance als out 加锁 不执行 http
参考文章:https://www.cnblogs.com/dingfangbo/p/5769501.html
一、lock 确保只有一个线程访问某个资源或某段代码。通俗的讲就是多个线程操作相同的锁对象,只能一个线程操作完毕,例外的线程才能继续访问锁定资源代码
如下代码:
1.修改锁定对象 的属性
RYAreaEmpPathWayVM areaEmpPathWayVM = instance.systemConfigDistributor.GetAreaEmpPathWayDetail(epcVM.TenantLayerCode, epcVM.epc_no);
if (areaEmpPathWayVM != null)
{
//由于区域雇员运动轨迹对象是动态对象故而锁定对象
lock (areaEmpPathWayVM)
{
//业务逻辑1:
#region 1、更新属性:最近刷卡4G设备号、最近进入区域的时间
areaEmpPathWayVM.recent_device4g_no = epcVM.ry_device4g_no;
areaEmpPathWayVM.recent_probe_time = epcVM.create_time;
#endregion
//业务逻辑2
#region .更新属性:第一次进入区域时间、最近刷卡4G设备号、最近进入区域的时间、最近进入区域ID、上一次被刷卡读头地址、最近刷卡读头地址
areaEmpPathWayVM.first_In_time = epcVM.create_time;
areaEmpPathWayVM.last_in_area_id = last_uhfPath.area_id;
areaEmpPathWayVM.last_probe_time = ALUtils.CloneObj<DateTime>(areaEmpPathWayVM.recent_probe_time);
//////////////////////////////////////////////////////////////////////////////////
areaEmpPathWayVM.recent_device4g_no = epcVM.ry_device4g_no;
areaEmpPathWayVM.recent_probe_time = epcVM.create_time;
areaEmpPathWayVM.recent_in_area_id = epcVM.access_Area_ID;
areaEmpPathWayVM.recent_uhf_portaddress = epcVM.uhf_portaddress;
#endregion
}
}
2.读取锁定对象的属性进行逻辑判断
RYAreaEmpPathWayVM tempRYAreaEmpPathWayVM = instance.systemConfigDistributor.GetAreaEmpPathWayDetail(alarmGenerateVM.org_layer_code, alarmGenerateVM.epc_no);
if (tempRYAreaEmpPathWayVM != null)
{
lock (tempRYAreaEmpPathWayVM)
{
if (!tempRYAreaEmpPathWayVM.recent_probe_time.HasValue || !tempRYAreaEmpPathWayVM.first_In_time.HasValue)
return false;
//如果最新读头第一次进入区域的时间与告警参数中第一次进入区域时间不相同,则雇员发生折返,不处理,否则处理
if (tempRYAreaEmpPathWayVM.first_In_time != alarmGenerateVM.create_time)
return false;
return true;
}
}
二、锁定的资源如果已经删除,需要二次判断对象资源是否存在
如下代码:
1.删除并发集合某个键值
ConcurrentDictionary<string, RyAreaInEmpVM> temp = instance.GetAreaInEmpMap(vm.org_layer_code, vm.area_id);
if (temp != null)
{
RyAreaInEmpVM aieDeleteVM;
temp.TryRemove(vm.ID, out aieDeleteVM);
}
2.如果多个线程访问已经锁定的资源会形成阻塞队列,这样当某个线程释放锁资源,还可以继续访问代码,但是实际已经被删除(键值),
所以二次加锁判断。
//查询条件:租户ID、区域ID、雇员ID 没有区域在场人员数据,则不执行 var aieVM = instance.areaController.GetAreaInEmpDetailByEmpId(ryAreaExitEmpVM.org_layer_code, ryAreaExitEmpVM.area_id, ryAreaExitEmpVM.emp_id); if (aieVM != null) { lock (aieVM) { aieVM = instance.areaController.GetAreaInEmpDetailByEmpId(ryAreaExitEmpVM.org_layer_code, ryAreaExitEmpVM.area_id, ryAreaExitEmpVM.emp_id); if (aieVM != null) { lock (aieVM) {
#region 1.区域告警消除
.......
#endregion
#region 2.1 删除离开区域雇员
.......
#endregion
}
} } }
标签:util div 文章 instance als out 加锁 不执行 http
原文地址:https://www.cnblogs.com/bwlluck/p/9510877.html