zoukankan      html  css  js  c++  java
  • Java多线程缓存器简单实现

    package com.charles.utils;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    
    public class CharlesCache {
    
        private Map<String, Object> data = new HashMap<String, Object>();
        private ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock();
        
        public static void main(String[] args) {
            
            CharlesCache cache = new CharlesCache();
    
            int i=0;
            //define 3 threads to load data;
            final int NUM= 3;
            while(i++ < NUM){
                new Thread(new Runnable(){
                    
                    @Override
                    public void run() {
                        while(true){
                            String key = Thread.currentThread().getName();
                            Object value = cache.getCachedData(key);
                            System.out.println(value+", "+key);
                            try{
                                Thread.sleep(2000);
                            }catch(InterruptedException e){
                                
                            }
                        }
                    }
                    
                }).start();
            }
            
        }
        
    
        public Object getCachedData(String key) {
    
            rwlock.readLock().lock();
            Object value = null;
            try {
                value = data.get(key);
                if (null != value) {
                    return value;
                } else {
                    /*
                     * Here, no data to get, Must release read lock before acquiring
                     * write lock to load data from somewhere like database.
                     */
                    rwlock.readLock().unlock();
                    rwlock.writeLock().lock();
                    /*
                     * Recheck state because another thread might have acquired
                     * write lock and changed state before we did.
                     */
                    try {
                        if (null == value) {
                            value = loadDataFromDB(key);
                            data.put(key, value);// put value into cache
                        }
                        // Downgrade by acquiring read lock before releasing write lock
                        rwlock.readLock().lock();
                    } finally {
                        rwlock.writeLock().unlock(); // Unlock write, still hold
                                                        // read
                    }
    
                }
            } finally {
                rwlock.readLock().unlock();
            }
    
            return value;
        }
    
        private Object loadDataFromDB(String key) {
            Object value = null;
            // need to be implemented...
            System.out.println(Thread.currentThread().getName()+" acquiring data..");
            value = "Hello";
            return value;
        }
    }
     
  • 相关阅读:
    k8s dashboard 配置使用kubeconfig文件登录
    Spring Cloud 专题之七:Sleuth 服务跟踪
    Spring Cloud 专题之六:bus 消息总线
    Spring Cloud专题之五:config 配置中心
    Docker Storage Driver:存储驱动
    Docker引擎升级教程
    Docker介绍及安装详解
    Autowired和Resource的区别和联系
    OLTP与OLAP
    转载-JAVA 关于JNI本地库加载
  • 原文地址:https://www.cnblogs.com/itachy/p/7206525.html
Copyright © 2011-2022 走看看