使用Lock,对不同商品加锁

本文介绍了一个使用ReentrantLock实现的多锁机制,该机制能够针对不同的键值进行加锁和解锁操作,确保线程安全。当某一线程对特定键值进行操作时,其他线程若尝试对该键值进行相同操作将会被阻塞,直到当前线程完成并释放锁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.boce.gbkutf;

 

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

import com.ibm.icu.util.Calendar;

//实现多key值多锁,如 key=12,已经加锁(HashMap 中存在),其它线程就不可以对key=12,进行操作,线程阻塞,需要等当前线程操作完成后,其它线程才可以操作.

package com.boce.gbkutf;

 

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

import com.ibm.icu.util.Calendar;

 

public class MutiLock {

 

  final Lock lock = new ReentrantLock();//锁对象  

  final Condition notFull  = lock.newCondition();//写线程条件   

  final Condition notEmpty = lock.newCondition();//读线程条件   

 //lock数量

  final Map<String,String> items = new HashMap<String,String>(100);//缓存队列  

 

  //锁定某个商品id

  public void put(String x) throws InterruptedException {  

    lock.tryLock(1000L, TimeUnit.SECONDS);  //锁定6秒钟

    try {  

      while (x == items.get(x)){//如果已经存在对象

        notFull.await();//阻塞写线程  

        System.out.println("time:"+Calendar.getInstance().getTimeInMillis());

      }

      

      items.put(x, x);//赋值   

      System.out.println("put:"+x);

    } finally {  

      lock.unlock();  

    }  

  }  

 

  //释放某个锁定的商品id

  public void take(String key) throws InterruptedException {  

    lock.tryLock(1000L, TimeUnit.SECONDS);    

    try {    

      notFull.signal();//唤醒写线程  

      items.remove(key);

      System.out.println("================get:"+key);

    } finally {  

      lock.unlock();  

    }  

  }

}

 

 

//测试类

 

package com.boce.gbkutf;

 

import java.util.Random;

 

public class ThreadTest implements Runnable{

 

private String key;

 

private MutiLock mutiLock;

 

public ThreadTest(String key, MutiLock mutiLock) {

super();

this.key = key;

this.mutiLock = mutiLock;

}

 

 

 

@Override

public void run() {

try {

mutiLock.put(key);

Random rand = new Random();

int data = rand.nextInt(2000);

System.out.println("key:"+key+";工作时间:"+data);

//模拟工作时长

Thread.sleep(data);

mutiLock.take(key);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 

 

}

 

}

 

 

 

 

package com.boce.gbkutf;

 

public class TestMain {

public static void main(String[] args) {

MutiLock mutiLock = new MutiLock();

 

for (int i = 0; i < 2; i++) {

String key = "123456";

ThreadTest test = new ThreadTest(key, mutiLock);

Thread test1 = new Thread(test);

test1.start();

 

String key1 = "123456"+i;

ThreadTest test11 = new ThreadTest(key1, mutiLock);

Thread test12 = new Thread(test11);

test12.start();

 

}

 

}

 

}

 

 

 

 

测试日志:

 

put:123456

put:1234561

key:123456;工作时间:660

put:1234560

key:1234561;工作时间:795

key:1234560;工作时间:1090

================get:123456

time:1489373212371

put:123456

key:123456;工作时间:419

================get:1234561

================get:1234560

================get:123456

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值