多线程访问 资源的安全控制

对于线程安全,大家都知道使用synchronized控制访问的资源,有变量安全、方法安全、块安全。

我这里有个需求是这样的,我这里作为服务端有很多客户端与我进行交互,服务端也会主动发消息给客户端,但是要求每次交互时只能有一个用户。也就是说发送一组信息、等待信息、处理信息返回时这个链路只能有一个人使用

 

也许大家马上会想到这样写:

Java代码   收藏代码
  1. synchronized (ThreadT.devIpsIsCanUse) {  
  2. }  

 

但是一旦这样写,在安全块内devIpsIsCanUse这个变量不能再被其他用户访问,因为此时这个资源是线程安全的,只能有一个线程进行访问。

如果此时我想访问不同线路,也是不行,因为一旦访问devIpsIsCanUse这个资源就会进行等待

可以这样写进行测试:

Java代码   收藏代码
  1. package com.hoo.mina;  
  2. import java.util.Map;  
  3. import java.util.concurrent.ConcurrentHashMap;  
  4. public class ThreadT {  
  5.     public static Map<String, Boolean> devIpsIsCanUse = new ConcurrentHashMap<String, Boolean>();  
  6.     public static void main(String[] args) {  
  7.         devIpsIsCanUse.put("testKey1"true);  
  8.         devIpsIsCanUse.put("testKey2"true);  
  9.         new Thread(new ServiceImpl(1)).start();  
  10.         new Thread(new ServiceImpl(2)).start();  
  11.   
  12.     }  
  13. }  
  14. class ServiceImpl implements Runnable {  
  15.     public int w;  
  16.     public ServiceImpl(int w) {  
  17.         this.w = w;  
  18.     }  
  19.     public void run() {  
  20.         while (true) {  
  21.             try {  
  22.                 synchronized (ThreadT.devIpsIsCanUse) {  
  23.                     if (ThreadT.devIpsIsCanUse.get("testKey" + w)){  
  24.                         ThreadT.devIpsIsCanUse.put("testKey" + w, false);  
  25.                         System.out.println(w + ":我来了:" + this.toString());  
  26.                         Thread.sleep(2 * 1000);  
  27.                         System.out.println(w + ":我走了:" + this.toString());  
  28.                         ThreadT.devIpsIsCanUse.put("testKey" + w, true);  
  29.                     }  
  30.                 }  
  31.             } catch (Exception e) {  
  32.                 e.printStackTrace();  
  33.             }  
  34.         }  
  35.     }  
  36. }  

 

运行后发现,对于资源1和资源2的访问是排队的,这明显不符合要求

 

改动很简单,只要把处理的代码放到安全块外即可,因为对于是否使用的判断处理是非常快的,在用就继续循环否则向下走即可

Java代码   收藏代码
  1. package com.hoo.mina;  
  2. import java.util.Map;  
  3. import java.util.concurrent.ConcurrentHashMap;  
  4. public class ThreadT {  
  5.     public static Map<String, Boolean> devIpsIsCanUse = new ConcurrentHashMap<String, Boolean>();  
  6.     public static void main(String[] args) {  
  7.         devIpsIsCanUse.put("testKey1"true);  
  8.         devIpsIsCanUse.put("testKey2"true);  
  9.         new Thread(new ServiceImpl(1)).start();  
  10.         new Thread(new ServiceImpl(2)).start();  
  11.   
  12.     }  
  13. }  
  14. class ServiceImpl implements Runnable {  
  15.     public int w;  
  16.     public ServiceImpl(int w) {  
  17.         this.w = w;  
  18.     }  
  19.     public void run() {  
  20.         while (true) {  
  21.             try {  
  22.                 synchronized (ThreadT.devIpsIsCanUse) {  
  23.                     if (ThreadT.devIpsIsCanUse.get("testKey" + w)){  
  24.                         ThreadT.devIpsIsCanUse.put("testKey" + w, false);  
  25.                     }else{  
  26.                         Thread.sleep(200);  
  27.                         continue;  
  28.                     }  
  29.                 }  
  30.                 System.out.println(w + ":我来了:" + this.toString());  
  31.                 Thread.sleep(2 * 1000);  
  32.                 System.out.println(w + ":我走了:" + this.toString());  
  33.                 ThreadT.devIpsIsCanUse.put("testKey" + w, true);  
  34.             } catch (Exception e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38.     }  
  39. }  

 

此时我们看到打印信息是资源1和资源2的访问是同时的,但是对于资源1的访问是需要排队的

 

那么此时我又在想,如果不加休眠,多个线程同时访问资源1时,如果A线程开始访问了且一直轮询,此时B再来访问,那么两个线程进安全块的顺序是必须A走完才能是B吗?

其实不是的在进安全块时,线程A和线程B是竞争的,而不是排队,可以这些看一下:

Java代码   收藏代码
  1. package com.hoo.mina;  
  2. import java.util.Map;  
  3. import java.util.concurrent.ConcurrentHashMap;  
  4. public class ThreadT {  
  5.     public static Map<String, Boolean> devIpsIsCanUse = new ConcurrentHashMap<String, Boolean>();  
  6.     public static void main(String[] args) {  
  7.         devIpsIsCanUse.put("testKey1"true);  
  8.         new Thread(new ServiceImpl(1)).start();  
  9.         new Thread(new ServiceImpl(1)).start();  
  10.         new Thread(new ServiceImpl(1)).start();  
  11.         new Thread(new ServiceImpl(1)).start();  
  12.     }  
  13. }  
  14. class ServiceImpl implements Runnable {  
  15.     public int w;  
  16.     public ServiceImpl(int w) {  
  17.         this.w = w;  
  18.     }  
  19.     public void run() {  
  20.         while (true) {  
  21.             try {  
  22.                 synchronized (ThreadT.devIpsIsCanUse) {  
  23.                     if (ThreadT.devIpsIsCanUse.get("testKey" + w)){  
  24.                         ThreadT.devIpsIsCanUse.put("testKey" + w, false);  
  25.                     }else{  
  26.                         Thread.sleep(500);  
  27.                         System.out.println(w + ":被占中:" + this.toString());  
  28.                         continue;  
  29.                     }  
  30.                 }  
  31.                 System.out.println(w + ":我来了:" + this.toString());  
  32.                 try {  
  33.                     Thread.sleep(1 * 1000);  
  34.                 } catch (Exception e) {  
  35.                 }  
  36.                 System.out.println(w + ":我走了:" + this.toString());  
  37.                 ThreadT.devIpsIsCanUse.put("testKey" + w, true);  
  38.             } catch (Exception e) {  
  39.                 e.printStackTrace();  
  40.             }  
  41.         }  
  42.     }  
  43. }  

 

看一下打印:

Java代码   收藏代码
  1. 1:我来了:ServiceImpl@14318bb  
  2. 1:被占中:ServiceImpl@1a758cb  
  3. 1:被占中:ServiceImpl@1b67f74  
  4. 1:我走了:ServiceImpl@14318bb  
  5. 1:被占中:ServiceImpl@1b67f74  
  6. 1:我来了:ServiceImpl@1b67f74  

 

可以明显看到他们是竞争关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值