java编写的一个缓存类cache类实例
来源:互联网 发布:js 实现map 编辑:程序博客网 时间:2024/04/29 22:43
/-----------------------------------------------
/ This class is a queue cache in multithreading,
/ it store object at tail,and remove object at
/ head,and it is thread-safe.
/-------------------------------------
*/
import java.util.LinkedList;
import java.lang.InterruptedException;
public class Cache
{
private LinkedList cache = new LinkedList();
private int cacheSize = 10;
public Cache()
{
}
public Cache(int cacheSize)
{
this.cacheSize = cacheSize;
}
public synchronized Object getCache() throws InterruptedException
{
System.out.println("Cache.getCache() method start!");
Object object = null;
while(true)
{
if(!cache.isEmpty())
{
object = cache.removeLast();
notifyAll();
break;
}
else
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Cache.getCache() method is interrupted and end!");
throw e;
}
}
}
System.out.println("Cache.getCache() method end!");
return object;
}
public synchronized void setCache(Object object) throws InterruptedException
{
System.out.println("Cache.setCache() method start!");
while(true)
{
if(cache.size() >= cacheSize)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Cache.setCache() method is interrupted and end!");
throw e;
}
}
else
{
cache.addFirst(object);
notifyAll();
break;
}
}
System.out.println("Cache.setCache() method end!");
}
public String toString()
{
return cache.toString();
}
public static void main(String arg[]){
SingletonObj so = SingletonObj.getInstance(); //其中SingletonObj是一个单态类
ArrayList al = so.getArrayListValue();
Hua c = new Hua();
try{
c.setCache(al);
}catch(Exception ex){
}
ArrayList al1 = new ArrayList();
try{
al1 = (ArrayList)c.getCache();
for(int i = 0;i<al1.size();i++)
System.out.println(al1.get(i));
}catch(Exception ex){
}
}
SingletonObj.java 程序
public class SingletonObj {
ArrayList al = new ArrayList();
private SingletonObj(){
//数据
for(int i = 0; i < 100; i ++){
al.add("wl"+i);
}
}
private static SingletonObj so = new SingletonObj();
public static SingletonObj getInstance(){
return so;
}
public ArrayList getArrayListValue(){
return al;
}
}
- java编写的一个缓存类cache类实例
- java编写的一个缓存类cache,适用于生产者-消费者模式
- java一个实现实例缓存的不可变类
- 一个简单的矩阵类编写实例
- 缓存Cache操作类
- Cache缓存类
- 缓存文件类 cache
- Spring Cache编写实例
- 自己做的一个缓存系统Cache
- 实现一个缓存实例的不可变类
- PHP编写一个限制实例化次数的类
- 一个简单的java缓存类
- [Java 缓存] Java Cache之 Guava Cache的简单应用.
- [Java 缓存] Java Cache之 Guava Cache的简单应用.
- 编写一个JAVA的队列类
- 编写一个JAVA的队列类
- 编写一个JAVA的队列类
- [PHP实例] 一个PHP缓存类
- 从数据库读出的JPG文件的字符流,转换成图片显示在页面上的相关代码
- 好的asp.net学习网站,blog
- is和as
- 用JSP实现类似资源管理器式的文件与目录的折叠(代码示例)
- 深入浅出关键字---把new说透
- java编写的一个缓存类cache类实例
- 关於档案/目录处理的指令
- 关於字串处理的指令:
- 网路上查询状况的指令:
- 网路指令:
- java的各种排序方法..
- Ear、Jar、War文件之间有何不同?它们分别使用在哪种环境下?
- class和struct
- struts,spring,hibernate的应用