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

对于线程安全,大家都知道使用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

 

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

 

请您到ITEYE网站看原创,谢谢!

http://cuisuqiang.iteye.com/ ! 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值