C#中的线程(二)线程同步基础 (读后感)

参考文章:https://www.cnblogs.com/dingfangbo/p/5769501.htmlhtml

1、lock 确保只有一个线程访问某个资源或某段代码。通俗的讲就是多个线程操做相同的锁对象,只能一个线程操做完毕,例外的线程才能继续访问锁定资源代码并发

以下代码:spa

1.修改锁定对象 的属性线程

RYAreaEmpPathWayVM areaEmpPathWayVM = instance.systemConfigDistributor.GetAreaEmpPathWayDetail(epcVM.TenantLayerCode, epcVM.epc_no);

  if (areaEmpPathWayVM != null)
  {
    //因为区域雇员运动轨迹对象是动态对象故而锁定对象
     lock (areaEmpPathWayVM)
       {

                 //业务逻辑1:

                  #region 一、更新属性:最近刷卡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.读取锁定对象的属性进行逻辑判断code

   

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;
         }
      }

2、锁定的资源若是已经删除,须要二次判断对象资源是否存在htm

以下代码:对象

1.删除并发集合某个键值blog

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 }  } } }
相关文章
相关标签/搜索