ReentrantLock 大访问量同步问题

gibby 2014-11-05 10:06:49
写了一段代码 在大访问量时不能保持 synchronized 的一个接一个效果 求解答
下面是JAVA代码

@Override
public Map kill() {
ReentrantLock lock = new ReentrantLock();
Transaction tx =null;
Jedis jedis =null;
Map resultMap = new HashMap();
try {
lock.lock();
System.out.println("thread name : "+Thread.currentThread().getName() +" work ");
jedis = RedisApi.getResource();
Good skGood = (Good)RedisApi.get(CommonDefinition.SEC_KILL_GOOD.getBytes());
int secQuantity = skGood.getSecQuantity();
if(secQuantity<=0){
resultMap.put("scuess", false);
}else{
tx = jedis.multi();
skGood.setSecQuantity(secQuantity-1);
RedisApi.set(CommonDefinition.SEC_KILL_GOOD, skGood);
tx.exec();
resultMap.put("scuess", true);
resultMap.put("kill_good_num", secQuantity);
}
//System.out.println(resultMap.get("kill_good_num")+"/ "+lock.getHoldCount()+" / "+lock.toString());
return resultMap;
} catch (Exception e) {
e.printStackTrace();
if(null!=tx){
tx.discard();
}
RedisApi.returnBrokenResource(jedis);
}finally{
RedisApi.returnResource(jedis);
lock.unlock();
System.out.println("thread name : "+Thread.currentThread().getName() +" finished ");
}
return null;
}


使用synchronized 50个线程访问的结果

thread name : http-bio-8080-exec-5 work
thread name : http-bio-8080-exec-5 finished
thread name : http-bio-8080-exec-7 work
thread name : http-bio-8080-exec-7 finished
thread name : http-bio-8080-exec-6 work
thread name : http-bio-8080-exec-6 finished
thread name : http-bio-8080-exec-3 work
thread name : http-bio-8080-exec-3 finished
thread name : http-bio-8080-exec-4 work
thread name : http-bio-8080-exec-4 finished
thread name : http-bio-8080-exec-9 work
thread name : http-bio-8080-exec-9 finished
thread name : http-bio-8080-exec-8 work
thread name : http-bio-8080-exec-8 finished
thread name : http-bio-8080-exec-10 work
thread name : http-bio-8080-exec-10 finished
thread name : http-bio-8080-exec-11 work
thread name : http-bio-8080-exec-11 finished
thread name : http-bio-8080-exec-12 work
thread name : http-bio-8080-exec-12 finished
thread name : http-bio-8080-exec-13 work
thread name : http-bio-8080-exec-13 finished
thread name : http-bio-8080-exec-14 work
thread name : http-bio-8080-exec-14 finished
thread name : http-bio-8080-exec-15 work
thread name : http-bio-8080-exec-15 finished
thread name : http-bio-8080-exec-16 work
thread name : http-bio-8080-exec-16 finished
thread name : http-bio-8080-exec-17 work
thread name : http-bio-8080-exec-17 finished
thread name : http-bio-8080-exec-19 work
thread name : http-bio-8080-exec-19 finished
thread name : http-bio-8080-exec-18 work
thread name : http-bio-8080-exec-18 finished
thread name : http-bio-8080-exec-20 work
thread name : http-bio-8080-exec-20 finished
thread name : http-bio-8080-exec-32 work
thread name : http-bio-8080-exec-32 finished
thread name : http-bio-8080-exec-8 work
thread name : http-bio-8080-exec-8 finished
thread name : http-bio-8080-exec-6 work
thread name : http-bio-8080-exec-6 finished
thread name : http-bio-8080-exec-4 work
thread name : http-bio-8080-exec-4 finished
thread name : http-bio-8080-exec-5 work
thread name : http-bio-8080-exec-5 finished
thread name : http-bio-8080-exec-19 work
thread name : http-bio-8080-exec-19 finished
thread name : http-bio-8080-exec-9 work
thread name : http-bio-8080-exec-9 finished
thread name : http-bio-8080-exec-5 work
thread name : http-bio-8080-exec-5 finished
thread name : http-bio-8080-exec-31 work
thread name : http-bio-8080-exec-31 finished
thread name : http-bio-8080-exec-3 work
thread name : http-bio-8080-exec-3 finished
thread name : http-bio-8080-exec-4 work
thread name : http-bio-8080-exec-4 finished
thread name : http-bio-8080-exec-14 work
thread name : http-bio-8080-exec-14 finished
thread name : http-bio-8080-exec-20 work
thread name : http-bio-8080-exec-20 finished
thread name : http-bio-8080-exec-10 work
thread name : http-bio-8080-exec-10 finished
thread name : http-bio-8080-exec-19 work
thread name : http-bio-8080-exec-19 finished
thread name : http-bio-8080-exec-21 work
thread name : http-bio-8080-exec-21 finished
thread name : http-bio-8080-exec-30 work
thread name : http-bio-8080-exec-30 finished
thread name : http-bio-8080-exec-12 work
thread name : http-bio-8080-exec-12 finished
thread name : http-bio-8080-exec-26 work
thread name : http-bio-8080-exec-26 finished
thread name : http-bio-8080-exec-22 work
thread name : http-bio-8080-exec-22 finished
thread name : http-bio-8080-exec-23 work
thread name : http-bio-8080-exec-23 finished
thread name : http-bio-8080-exec-13 work
thread name : http-bio-8080-exec-13 finished
thread name : http-bio-8080-exec-28 work
thread name : http-bio-8080-exec-28 finished
thread name : http-bio-8080-exec-5 work
thread name : http-bio-8080-exec-5 finished
thread name : http-bio-8080-exec-8 work
thread name : http-bio-8080-exec-8 finished
thread name : http-bio-8080-exec-3 work
thread name : http-bio-8080-exec-3 finished
thread name : http-bio-8080-exec-18 work
thread name : http-bio-8080-exec-18 finished
thread name : http-bio-8080-exec-14 work
thread name : http-bio-8080-exec-14 finished
thread name : http-bio-8080-exec-20 work
thread name : http-bio-8080-exec-20 finished
thread name : http-bio-8080-exec-10 work
thread name : http-bio-8080-exec-10 finished
thread name : http-bio-8080-exec-19 work
thread name : http-bio-8080-exec-19 finished
thread name : http-bio-8080-exec-21 work
thread name : http-bio-8080-exec-21 finished


以下是用上面的ReentrantLock 50个线程访问的效果

thread name : http-bio-8080-exec-4 work
thread name : http-bio-8080-exec-5 work
thread name : http-bio-8080-exec-3 work
thread name : http-bio-8080-exec-6 work
thread name : http-bio-8080-exec-7 work
thread name : http-bio-8080-exec-4 finished
thread name : http-bio-8080-exec-6 finished
thread name : http-bio-8080-exec-5 finished
thread name : http-bio-8080-exec-3 finished
thread name : http-bio-8080-exec-7 finished
thread name : http-bio-8080-exec-9 work
thread name : http-bio-8080-exec-8 work
thread name : http-bio-8080-exec-9 finished
thread name : http-bio-8080-exec-8 finished
thread name : http-bio-8080-exec-10 work
thread name : http-bio-8080-exec-10 finished
thread name : http-bio-8080-exec-1 work
thread name : http-bio-8080-exec-1 finished
thread name : http-bio-8080-exec-11 work
thread name : http-bio-8080-exec-11 finished
thread name : http-bio-8080-exec-12 work
thread name : http-bio-8080-exec-12 finished
thread name : http-bio-8080-exec-13 work
thread name : http-bio-8080-exec-13 finished
thread name : http-bio-8080-exec-14 work
thread name : http-bio-8080-exec-14 finished
thread name : http-bio-8080-exec-16 work
thread name : http-bio-8080-exec-16 finished
thread name : http-bio-8080-exec-15 work
thread name : http-bio-8080-exec-15 finished
thread name : http-bio-8080-exec-14 work
thread name : http-bio-8080-exec-14 finished
thread name : http-bio-8080-exec-2 work
thread name : http-bio-8080-exec-2 finished
thread name : http-bio-8080-exec-24 work
thread name : http-bio-8080-exec-24 finished
thread name : http-bio-8080-exec-7 work
thread name : http-bio-8080-exec-11 work
thread name : http-bio-8080-exec-7 finished
thread name : http-bio-8080-exec-11 finished
thread name : http-bio-8080-exec-19 work
thread name : http-bio-8080-exec-19 finished
thread name : http-bio-8080-exec-4 work
thread name : http-bio-8080-exec-4 finished
thread name : http-bio-8080-exec-11 work
thread name : http-bio-8080-exec-11 finished
thread name : http-bio-8080-exec-15 work
thread name : http-bio-8080-exec-15 finished
thread name : http-bio-8080-exec-3 work
thread name : http-bio-8080-exec-3 finished
thread name : http-bio-8080-exec-7 work
thread name : http-bio-8080-exec-7 finished
thread name : http-bio-8080-exec-18 work
thread name : http-bio-8080-exec-18 finished
thread name : http-bio-8080-exec-18 work
thread name : http-bio-8080-exec-18 finished
thread name : http-bio-8080-exec-1 work
thread name : http-bio-8080-exec-1 finished
thread name : http-bio-8080-exec-17 work
thread name : http-bio-8080-exec-17 finished
thread name : http-bio-8080-exec-25 work
thread name : http-bio-8080-exec-25 finished
thread name : http-bio-8080-exec-2 work
thread name : http-bio-8080-exec-2 finished
thread name : http-bio-8080-exec-7 work
thread name : http-bio-8080-exec-7 finished
thread name : http-bio-8080-exec-23 work
thread name : http-bio-8080-exec-23 finished
thread name : http-bio-8080-exec-13 work
thread name : http-bio-8080-exec-13 finished
thread name : http-bio-8080-exec-19 work
thread name : http-bio-8080-exec-19 finished
thread name : http-bio-8080-exec-12 work
thread name : http-bio-8080-exec-12 finished
thread name : http-bio-8080-exec-6 work
thread name : http-bio-8080-exec-6 finished
thread name : http-bio-8080-exec-4 work
thread name : http-bio-8080-exec-4 finished
thread name : http-bio-8080-exec-21 work
thread name : http-bio-8080-exec-21 finished
thread name : http-bio-8080-exec-18 work
thread name : http-bio-8080-exec-18 finished
thread name : http-bio-8080-exec-1 work
thread name : http-bio-8080-exec-1 finished
thread name : http-bio-8080-exec-17 work
thread name : http-bio-8080-exec-17 finished
......

如果想要用lock 实现按顺序的一个接一个完成要如果写 求解答
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gibby 2014-11-18
  • 打赏
  • 举报
回复
我看了文档说 lock方法就是阻塞的了跟同步关键字一样
gibby 2014-11-18
  • 打赏
  • 举报
回复
我看了文档说 lock方法就是阻塞的了跟同步关键字一样
晓风吹雾 2014-11-05
  • 打赏
  • 举报
回复
ReentrantLock 是重入所, lock 后其他的线程 还是会进入,一般配合condition, 重入所一般为了提高效率,是其他的线程不会呆呆的block,而是 有更多的选择余地。 你要做成纯粹的窜行执行,为什不用 synchronized 一般 ReentrantLock 配合 condition 下面是配合 condition,模仿synchronized窜行执行的效果。 这样没什么意义

package jp.co.nec.aim.demo.servlet;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Entrance {

	private static final ReentrantLock lock = new ReentrantLock(false);
	private static final Condition c = lock.newCondition();

	public static void main(String[] args) {
		lock.lock();
		try {
			c.await();
			// do something
			System.out.print(11);
			c.notify();
		} catch (InterruptedException e) {
		} finally {
			lock.unlock();
		}
	}
}

gibby 2014-11-05
  • 打赏
  • 举报
回复
引用 2 楼 zhouren1314 的回复:
加上 一个 boolean变量试试,每次操作时判断一下,只是思路,不知道能行么?
判断什么?
乔不思 2014-11-05
  • 打赏
  • 举报
回复
加上 一个 boolean变量试试,每次操作时判断一下,只是思路,不知道能行么?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧