A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jonn 高级黑马   /  2013-3-7 22:34  /  1427 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Collection;
  2.         import java.util.HashMap;
  3.         import java.util.Iterator;
  4.         import java.util.Map;
  5.         import java.util.Random;
  6.         import java.util.Set;
  7.         import java.util.concurrent.locks.ReentrantReadWriteLock;
  8.        
  9.         public class CacheItem {
  10.        
  11.                 private static Map<String ,Object> cache = new HashMap<String ,Object>();
  12.                 private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
  13.                 public static Object getObject(String key,Object target){
  14.                         readWriteLock.readLock().lock();
  15.                         Object polixy = null;       
  16.                         try{
  17.                                 polixy = cache.get(key);
  18.                                 if(polixy==null){
  19.                                         readWriteLock.readLock().unlock();       
  20.                                         readWriteLock.writeLock().lock();
  21.                                         try{
  22.                                         if(polixy==null){
  23.                                           polixy = target;
  24.                                           cache.put(key, polixy);
  25.                                         }       
  26.                                  }
  27.                                   finally{
  28.                                         readWriteLock.writeLock().unlock();
  29.                                   }
  30.                            }
  31.                                 readWriteLock.readLock().lock();               
  32.                         }
  33.                         finally{
  34.                                 readWriteLock.readLock().unlock();
  35.                         }       
  36.                  return polixy;                       
  37.           }
  38.                
  39.                 public static void main(String[] args) {
  40.                        
  41.                   for(int i=0;i<10;i++){
  42.                          final int count = i;
  43.                          new Thread(new Runnable(){
  44.        
  45.                                 @Override
  46.                                 public void run() {
  47.                                          System.out.println(Thread.currentThread().getName()+" ::: "+count+" ::: "+CacheItem.getObject(String.valueOf(count), new Random().nextInt(1000)));       
  48.                                 }
  49.                                  
  50.                          }).start();  
  51.                   }
  52.                                                             
  53.                          for(Map.Entry<String, Object>  itdemo : cache.entrySet()){
  54.                                  System.out.println("key="+itdemo.getKey()+","+"value="+itdemo.getValue());  // 结果不是预期想要了,要么少了几个element ,要么并发修改异常.....崩溃了
  55.                          }
  56.                          
  57.                          System.out.println(cache.toString());
复制代码
若能很好了解决方法,有奖励哟....{:soso_e113:}


3 个回复

倒序浏览
注意:程序代码后面少了两个  } }  ,是我的失误...{:soso_e127:}
回复 使用道具 举报
你是不是要这样的结果?
  1. package ClassTest;

  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Random;
  7. import java.util.Set;
  8. import java.util.concurrent.locks.ReentrantReadWriteLock;

  9. public class CacheItem {

  10.         private static Map<String, Object> cache = new HashMap<String, Object>();
  11.         private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

  12.         public static Object getObject(String key, Object target) {
  13.                 readWriteLock.readLock().lock();
  14.                 Object polixy = null;
  15.                 try {
  16.                         polixy = cache.get(key);
  17.                         if (polixy == null) {
  18.                                 readWriteLock.readLock().unlock();
  19.                                 readWriteLock.writeLock().lock();
  20.                                 try {
  21.                                         if (polixy == null) {
  22.                                                 polixy = target;
  23.                                                 cache.put(key, polixy);
  24.                                         }
  25.                                 } finally {
  26.                                         readWriteLock.writeLock().unlock();
  27.                                 }
  28.                         }
  29.                         readWriteLock.readLock().lock();
  30.                 } finally {
  31.                         readWriteLock.readLock().unlock();
  32.                 }
  33.                 return polixy;
  34.         }

  35.         //将循环封装到一个线程里
  36.         class Thr extends Thread {
  37.                 public void run() {
  38.                         for (int i = 0; i < 10; i++) {
  39.                                 final int count = i;
  40.                                 new Thread(new Runnable() {

  41.                                         @Override
  42.                                         public void run() {
  43.                                                 System.out.println(Thread.currentThread().getName()
  44.                                                                 + " ::: "
  45.                                                                 + count
  46.                                                                 + " ::: "
  47.                                                                 + CacheItem.getObject(String.valueOf(count),
  48.                                                                                 new Random().nextInt(1000)));
  49.                                         }

  50.                                 }).start();
  51.                         }
  52.                 }
  53.         }

  54.         public static void main(String[] args) throws InterruptedException {

  55.                 //让这个线程优先执行
  56.                 Thread t = new CacheItem().new Thr();
  57.                 t.run();
  58.                 t.join();

  59.                 //由于线程必须执行完集合中值才完整所以必须让这个在循环之后执行
  60.                 for (Map.Entry<String, Object> itdemo : cache.entrySet()) {
  61.                         System.out.println("key=" + itdemo.getKey() + "," + "value="
  62.                                         + itdemo.getValue()); // 结果不是预期想要了,要么少了几个element
  63.                                                                                         // ,要么并发修改异常.....崩溃了
  64.                 }

  65.                 System.out.println(cache.toString());
  66.         }
  67. }
复制代码
回复 使用道具 举报
许鑫星 发表于 2013-3-8 00:03
你是不是要这样的结果?

貌似这种写法还行....,不过我用CountDownLatch,可惜,我给不了金币...让斑竹给你技术分吧;P
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马