java用户评论代码 javalibrary最佳评论( 五 )


private final long createTime;
public LiveCache(int cacheMillis, T element) {this.cacheMillis = cacheMillis;this.element = element;this.createTime = System.currentTimeMillis();
}
// 获取缓存对象
public T getElement() {long currentTime = System.currentTimeMillis();if(cacheMillis0currentTime - createTimecacheMillis) {return null;
} else {return element;
}
}
// 获取缓存对象 , 忽略缓存时间有效性
public T getElementIfNecessary() {return element;
}
}
public static void main(String[] args) {int cacheMilis = 1000 ;
LiveCacheObject liveCache = new LiveCache(cacheMilis, new Object()) ;
liveCache.getElement() ;
liveCache.getElementIfNecessary() ;
}
有效时间内 , 缓存单个对象,可异步刷新
@FunctionalInterfacepublic interface LiveFetchT {// 刷新缓存接口T fetch() ;
}
public class LiveManagerT {// 缓存时间
private int cacheMillis;// 缓存对象
private LiveCacheT liveCache;// 刷新缓存的对象
private LiveFetchT liveFetch ;
private Logger logger = LoggerFactory.getLogger(LiveManager.class) ;
// 刷新缓存开关
private boolean refresh = false ;
public LiveManager(int cacheMillis, LiveFetchT liveFetch) {this.cacheMillis = cacheMillis ;this.liveFetch = liveFetch ;
}
/**
* fetch cache ; if cache expired , synchronous fetch
* @return
*/
public T getCache() {
initLiveCache();
if(liveCache != null) {
T t;if((t= liveCache.getElement()) != null) {return t ;
} else {
t = liveFetch.fetch() ;if(t != null) {
liveCache = new LiveCacheT(cacheMillis, t) ;return t ;
}
}
}
return null ;
}
/**
* fetch cache ; if cache expired , return old cache and asynchronous fetch
* @return
*/
public T getCacheIfNecessary() {
initLiveCache();
if(liveCache != null) {
T t;if((t= liveCache.getElement()) != null) {return t ;
} else {
refreshCache() ;return liveCache.getElementIfNecessary() ;
}
}
return null ;
}
/**
* init liveCache*/
private void initLiveCache() {if(liveCache == null) {
T t = liveFetch.fetch() ;if(t != null) {
liveCache = new LiveCacheT(cacheMillis, t) ;
}
}
}
/**
* asynchronous refresh cache*/
private void refreshCache() {
if(refresh)return ;
refresh = true ;try {
Thread thread = new Thread(() - {try {
T t = liveFetch.fetch();if (t != null) {
liveCache = new LiveCache(cacheMillis, t);
}
} catch (Exception e){
logger.error("LiveManager.refreshCache thread error.", e);
} finally {
refresh = false ;
}
}) ;
thread.start();
} catch (Exception e) {
logger.error("LiveManager.refreshCache error.", e);
}
}
}
public class Test {
public static void main(String[] args) {int cacheMilis = 1000 ;
LiveManagerObject liveManager = new LiveManager(cacheMilis,() - new Test().t1()) ;
liveManager.getCache() ;
liveManager.getCacheIfNecessary() ;
}
public Object t1(){
return new Object() ;
}
}
java接受用户输入代码import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* System.in 标准输入流 。是从键盘获取数据的
*
* 键盘录入数据:
*A:main方法的args接收参数 。
*java HelloWorld hello world java
*B:Scanner(JDK5以后的)
*Scanner sc = new Scanner(System.in);
*String s = sc.nextLine();
*int x = sc.nextInt()
*C:通过字符缓冲流包装标准输入流实现
*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
*/
public class SystemInDemo {

推荐阅读