黑马程序员技术交流社区
标题:
CachePolixy 缓存代理读写锁...
[打印本页]
作者:
jonn
时间:
2013-3-7 22:34
标题:
CachePolixy 缓存代理读写锁...
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class CacheItem {
private static Map<String ,Object> cache = new HashMap<String ,Object>();
private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public static Object getObject(String key,Object target){
readWriteLock.readLock().lock();
Object polixy = null;
try{
polixy = cache.get(key);
if(polixy==null){
readWriteLock.readLock().unlock();
readWriteLock.writeLock().lock();
try{
if(polixy==null){
polixy = target;
cache.put(key, polixy);
}
}
finally{
readWriteLock.writeLock().unlock();
}
}
readWriteLock.readLock().lock();
}
finally{
readWriteLock.readLock().unlock();
}
return polixy;
}
public static void main(String[] args) {
for(int i=0;i<10;i++){
final int count = i;
new Thread(new Runnable(){
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" ::: "+count+" ::: "+CacheItem.getObject(String.valueOf(count), new Random().nextInt(1000)));
}
}).start();
}
for(Map.Entry<String, Object> itdemo : cache.entrySet()){
System.out.println("key="+itdemo.getKey()+","+"value="+itdemo.getValue()); // 结果不是预期想要了,要么少了几个element ,要么并发修改异常.....崩溃了
}
System.out.println(cache.toString());
复制代码
若能很好了解决方法,有奖励哟....{:soso_e113:}
作者:
jonn
时间:
2013-3-7 23:16
注意:程序代码后面少了两个 } } ,是我的失误...{:soso_e127:}
作者:
许鑫星
时间:
2013-3-8 00:03
你是不是要这样的结果?
package ClassTest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class CacheItem {
private static Map<String, Object> cache = new HashMap<String, Object>();
private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public static Object getObject(String key, Object target) {
readWriteLock.readLock().lock();
Object polixy = null;
try {
polixy = cache.get(key);
if (polixy == null) {
readWriteLock.readLock().unlock();
readWriteLock.writeLock().lock();
try {
if (polixy == null) {
polixy = target;
cache.put(key, polixy);
}
} finally {
readWriteLock.writeLock().unlock();
}
}
readWriteLock.readLock().lock();
} finally {
readWriteLock.readLock().unlock();
}
return polixy;
}
//将循环封装到一个线程里
class Thr extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
final int count = i;
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()
+ " ::: "
+ count
+ " ::: "
+ CacheItem.getObject(String.valueOf(count),
new Random().nextInt(1000)));
}
}).start();
}
}
}
public static void main(String[] args) throws InterruptedException {
//让这个线程优先执行
Thread t = new CacheItem().new Thr();
t.run();
t.join();
//由于线程必须执行完集合中值才完整所以必须让这个在循环之后执行
for (Map.Entry<String, Object> itdemo : cache.entrySet()) {
System.out.println("key=" + itdemo.getKey() + "," + "value="
+ itdemo.getValue()); // 结果不是预期想要了,要么少了几个element
// ,要么并发修改异常.....崩溃了
}
System.out.println(cache.toString());
}
}
复制代码
作者:
jonn
时间:
2013-3-8 10:52
许鑫星 发表于 2013-3-8 00:03
你是不是要这样的结果?
貌似这种写法还行....,不过我用CountDownLatch,可惜,我给不了金币...让斑竹给你技术分吧;P
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2