今天阿里巴巴让我在线编程

与阿里巴巴的面试也有好几次了,最早是在好几年前,我初出茅庐,电话面试啥也不懂。都是心酸史。今天阿里巴巴让我在线写一段程序,在一个网页上,他们能在线看见我在写什么。

题目是:实现一个缓存容器,支持容量指定和超出容量按照热度淘汰,同时尽可能保证多线程环境的读写性能,不能使用外部三方库


给我的时间限制是一个小时:我大概写了如下程序,不能运行,没有调试,也没有太多的深入思考,时间有限啊。现在把代码贴出来了。

//缓存接口 
public interface Cache{
     Object get(String key);
     boolean delete(String key);
     boolean put(String key,Object value);
     boolean clear();
 }
 //带热度的值
 public class HotMatrix{
     private AutomicLong hot=new AutomicLong(0);
     private Object data;
     
     public void setData(Object data){
         this.data=data;
     }
     
     public Object getData(){
         return this.data;
     }
     
     public void incrementHot(){
         hot.incrementAndGet();
     }
     
     public long getHot(){
         return this.hot.longValue();
     }
 }
 //一个简单缓存实现
 public class MyCache implements Cache {
    private long maxSize;
    private volatile AutomicLong currentSize=0L;
    private Map data=new ConcurrentHashMap(maxSize);
    
    public MyCache(int size){
        this.maxSize=size;
    }
    
    private synchronized void deleteLeastHottest(){
        Collection values=data.values();
        Collections.sort(values,new Comparator(){  
            @Override  
            public int compare(HotMatrix b1, HotMatrix b2) {  
                return b1.getHot()>b2.getHot();
            }  
              
        }); 
        
        Iterator it=values.Iterator;
        while(it.hasNext()){
            HotMatrix val=it.next();
            if(!it.hasNext()){
                values.remove(val);
            }
        }
    }
    public Object get(String key){
        HotMatrix hotValue=data.get(key);
        Object value=hotValue.getData();
        hotValue.incrementAndGet();
        return value;
    }
    public boolean delete(String key){
        data.remove(key);
        return true;
    }
    public boolean put(String key,Object value){
        HotMatrix hotValue=new HotMatrix();
        hotValue.setData(value);
        if(data.contains(key){
            data.put(key,hotValue);
            return true;
        }
        if(currentSize>=maxSize){
            deleteLeastHottest();
            
        }
        data.put(key,hotValue);
        currentSize.incrementAndGet();
        return true;
    }
    public boolean clear(){
        data.clear();
        return true;
    }
     
 }
 //缓存管理接口
 public interface CacheManager{
     void setCache(Cache cache);
     Object get(String key);
     boolean delete(String key);
     boolean put(String key,Object value);
     boolean clear();
 }
 //一个简单的缓存管理实现
 public class MyCacheManager implements CacheManager{
     private static volatile MyCacheManager instance;
     private Cache cache;
     private Lock lock=new Reentrantlock();
     
     private MyCacheManager(){}
     
     private MyCacheManager(Cache cache){
         this.cache=cache;
     }
     
     public static CacheManager getInstance(){
         if(instance==null){
             lock.lock()
             try{
                 if(intance==null){
                     instance=new MyCacheManager()
                 }
             }finally{
                 lock.unlock();
             }
         }
         return instance;
     }
     
     public void setCache(Cache cache){
         this.cache=cache;
     }
     
     public Object get(String key){
         return this.cache.get(key);
     }
     public boolean delete(String key){
         return this.cache.delete(key)
     }
     public boolean put(String key,Object value){
         return this.cache.put(key,value);
         
     }
     public boolean clear(){
         return this.cache.clear();
     }
     
 }
     
     
//测试
public class Test{
    public static void main(String[]args){
        CacheManager manager= MyCacheManager.getInstance();
        manager.setCache(new MyCache(10L));
        manager.put("java","java编程思想");
        manager.put("java","java编程思想");
        manager.put("java","java编程思想");
        manager.put("java","java编程思想");
        
        Object value=manager.get("java");
        manager.delete("java");
        manager.put("java","java编程思想");
        manager.clear();
        
        Thread t1=new Thread(new Runnable(){
           public void run(){
               manager.put("java","java编程思想");
                manager.put("java","java编程思想");
                manager.put("java","java编程思想");
                manager.put("java","java编程思想");
                manager.put("scala","scala编程思想");
                Object value=manager.get("java");
                manager.delete("java");
                manager.put("java","java编程思想");
                manager.clear();
           } 
        });
        
        Thread t2=new Thread(new Runnable(){
           public void run(){
               manager.put("java","java编程思想");
                manager.put("java","java编程思想");
                manager.put("java","java编程思想");
                manager.put("java","java编程思想");
                manager.delete("java");
                Object value=manager.get("java");
                manager.put("scala","scala编程思想")
                value=manager.get("scala");
                manager.put("java","java编程思想");
                manager.clear();
           } 
        });
        
        t1.start();
        t2.start();
    }
}


你可能感兴趣的:(面试,java面试题)