这个是上读写锁的部分
public class LockMap {
private static HashMap<String,Object> map=new HashMap<String, Object>();
private static ReentrantReadWriteLock lock=new ReentrantReadWriteLock();
private static Lock read=lock.readLock();
private static Lock write=lock.writeLock();
public Object get(String key) throws InterruptedException {
read.lock();
try{
Object obj=map.get(key);
return obj;
}finally {
read.unlock();
}
}
public void put(String key,Object value) throws InterruptedException {
write.lock();
try{
map.put(key,value);
}finally {
write.unlock();
}
}
}
这是创建线程运行的部分
public class LockMapTest {
private static SimpleDateFormat format=new SimpleDateFormat("HH:mm:ss:sss");
private static LockMap map=new LockMap();
public static void main(String[] args) throws InterruptedException {
new Thread(new Runner("w"),"Write-Thread1").start();
new Thread(new Runner("r"),"Read-Thread1").start();
new Thread(new Runner("r"),"Read-Thread2").start();
new Thread(new Runner("r"),"Read-Thread3").start();
new Thread(new Runner("r"),"Read-Thread4").start();
}
public static class Runner implements Runnable{
private String readWrite;
public Runner(String readWrite){
this.readWrite=readWrite;
}
public void run() {
while (true){
if ("w".equals(readWrite)){
try {
System.out.println(format.format(new Date())+" Current thread "+Thread.currentThread().getName()+" writing ");
map.put("first",UUID.randomUUID().toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if ("r".equals(readWrite)){
try {
System.out.println(format.format(new Date())+" Current thread "+Thread.currentThread().getName()+" get value "+map.get("first"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
运行以后,线程运行效果图
运行以后,四条读线程总是只有一条线程能运行,并不能同时运行,这是怎么回事吖
读锁 不会互斥另外一个读锁,但是如果一个线程对一个资源加了读锁,另外一个线程是不能添加写锁的 。
这样,在一个线程去添加写锁的时候,那么就必须等待其他线程将读锁释放,这样就会存在等待了 。
因为你线程里面没有做什么事情,所以线程看不太出来同时运行的情况
按如下,你就可以看到几个读锁线程在一起运行。
System.out.println(" Current thread "+Thread.currentThread().getName()+" [start] get value "+mLockMap.get("first"));
try{
Thread.sleep(100);
System.out.println(" Current thread "+Thread.currentThread().getName()+" [end] get value "+mLockMap.get("first"));
}
catch(Exception e) {
e.printStackTrace();
}
不知道你的线程效果图是用什么弄出来的 。 而且你的这个测试并不是测试一个线程锁住,因为你是一直在循环锁住并且释放。 试试下面这个
public class LockMap {
private static HashMap<String, String> map = new HashMap<String, String>();
private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static Lock read = lock.readLock();
private static Lock write = lock.writeLock();
public String get(String key) throws InterruptedException {
read.lock();
try {
String obj = map.get(key);
System.out.println(Thread.currentThread().getName() + "ST");
Thread.sleep(1000000);
System.out.println(Thread.currentThread().getName() + "END");
return obj;
} finally {
read.unlock();
}
}
public void put(String key, String value) throws InterruptedException {
write.lock();
try {
map.put(key, value);
System.out.println(Thread.currentThread().getName() + "ST");
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + "END");
} finally {
write.unlock();
}
}
}
运行结果如下 :
10:47:13:013 Current thread Write-Thread0 writing
Write-Thread0ST
Write-Thread0END
10:47:18:018 Current thread Write-Thread0 writing
Read-Thread3ST
Read-Thread4ST
Read-Thread1ST
Read-Thread2ST
很明显,读锁与写锁是互斥的,但是读锁与读锁并没有互斥