对于线程安全,大家都知道使用synchronized控制访问的资源,有变量安全、方法安全、块安全。
我这里有个需求是这样的,我这里作为服务端有很多客户端与我进行交互,服务端也会主动发消息给客户端,但是要求每次交互时只能有一个用户。也就是说发送一组信息、等待信息、处理信息返回时这个链路只能有一个人使用。
也许大家马上会想到这样写:
synchronized (ThreadT.devIpsIsCanUse) { }
但是一旦这样写,在安全块内devIpsIsCanUse这个变量不能再被其他用户访问,因为此时这个资源是线程安全的,只能有一个线程进行访问。
如果此时我想访问不同线路,也是不行,因为一旦访问devIpsIsCanUse这个资源就会进行等待
可以这样写进行测试:
package com.hoo.mina; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ThreadT { public static Map<String, Boolean> devIpsIsCanUse = new ConcurrentHashMap<String, Boolean>(); public static void main(String[] args) { devIpsIsCanUse.put("testKey1", true); devIpsIsCanUse.put("testKey2", true); new Thread(new ServiceImpl(1)).start(); new Thread(new ServiceImpl(2)).start(); } } class ServiceImpl implements Runnable { public int w; public ServiceImpl(int w) { this.w = w; } public void run() { while (true) { try { synchronized (ThreadT.devIpsIsCanUse) { if (ThreadT.devIpsIsCanUse.get("testKey" + w)){ ThreadT.devIpsIsCanUse.put("testKey" + w, false); System.out.println(w + ":我来了:" + this.toString()); Thread.sleep(2 * 1000); System.out.println(w + ":我走了:" + this.toString()); ThreadT.devIpsIsCanUse.put("testKey" + w, true); } } } catch (Exception e) { e.printStackTrace(); } } } }
运行后发现,对于资源1和资源2的访问是排队的,这明显不符合要求
改动很简单,只要把处理的代码放到安全块外即可,因为对于是否使用的判断处理是非常快的,在用就继续循环否则向下走即可
package com.hoo.mina; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ThreadT { public static Map<String, Boolean> devIpsIsCanUse = new ConcurrentHashMap<String, Boolean>(); public static void main(String[] args) { devIpsIsCanUse.put("testKey1", true); devIpsIsCanUse.put("testKey2", true); new Thread(new ServiceImpl(1)).start(); new Thread(new ServiceImpl(2)).start(); } } class ServiceImpl implements Runnable { public int w; public ServiceImpl(int w) { this.w = w; } public void run() { while (true) { try { synchronized (ThreadT.devIpsIsCanUse) { if (ThreadT.devIpsIsCanUse.get("testKey" + w)){ ThreadT.devIpsIsCanUse.put("testKey" + w, false); }else{ Thread.sleep(200); continue; } } System.out.println(w + ":我来了:" + this.toString()); Thread.sleep(2 * 1000); System.out.println(w + ":我走了:" + this.toString()); ThreadT.devIpsIsCanUse.put("testKey" + w, true); } catch (Exception e) { e.printStackTrace(); } } } }
此时我们看到打印信息是资源1和资源2的访问是同时的,但是对于资源1的访问是需要排队的
那么此时我又在想,如果不加休眠,多个线程同时访问资源1时,如果A线程开始访问了且一直轮询,此时B再来访问,那么两个线程进安全块的顺序是必须A走完才能是B吗?
其实不是的在进安全块时,线程A和线程B是竞争的,而不是排队,可以这些看一下:
package com.hoo.mina; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ThreadT { public static Map<String, Boolean> devIpsIsCanUse = new ConcurrentHashMap<String, Boolean>(); public static void main(String[] args) { devIpsIsCanUse.put("testKey1", true); new Thread(new ServiceImpl(1)).start(); new Thread(new ServiceImpl(1)).start(); new Thread(new ServiceImpl(1)).start(); new Thread(new ServiceImpl(1)).start(); } } class ServiceImpl implements Runnable { public int w; public ServiceImpl(int w) { this.w = w; } public void run() { while (true) { try { synchronized (ThreadT.devIpsIsCanUse) { if (ThreadT.devIpsIsCanUse.get("testKey" + w)){ ThreadT.devIpsIsCanUse.put("testKey" + w, false); }else{ Thread.sleep(500); System.out.println(w + ":被占中:" + this.toString()); continue; } } System.out.println(w + ":我来了:" + this.toString()); try { Thread.sleep(1 * 1000); } catch (Exception e) { } System.out.println(w + ":我走了:" + this.toString()); ThreadT.devIpsIsCanUse.put("testKey" + w, true); } catch (Exception e) { e.printStackTrace(); } } } }
看一下打印:
1:我来了:ServiceImpl@14318bb 1:被占中:ServiceImpl@1a758cb 1:被占中:ServiceImpl@1b67f74 1:我走了:ServiceImpl@14318bb 1:被占中:ServiceImpl@1b67f74 1:我来了:ServiceImpl@1b67f74
可以明显看到他们是竞争关系
推荐您阅读更多有关于“ 线程 安全 资源控制 synchronized ConcurrentHashMap ”的文章
Java小强
未曾清贫难成人,不经打击老天真。
自古英雄出炼狱,从来富贵入凡尘。
发表评论: