java编写的一个缓存类cache,适用于生产者-消费者模式
2007-04-12 15:28
555 查看
/*
/------------------------
/ author: zhouhp
/ date: 2007-4-10
/ email: zhp_hlm@163.com
/ MSN: zhp_hlm@163.com
/-----------------------------------------------
/ This class is a queue cache in multithreading,
/ it store object at tail,and remove object at
/ head,and it is thread-safe.
/-------------------------------------
*/
package com.zhp.util;
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();
}
}
/------------------------
/ author: zhouhp
/ date: 2007-4-10
/ email: zhp_hlm@163.com
/ MSN: zhp_hlm@163.com
/-----------------------------------------------
/ This class is a queue cache in multithreading,
/ it store object at tail,and remove object at
/ head,and it is thread-safe.
/-------------------------------------
*/
package com.zhp.util;
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();
}
}
相关文章推荐
- java中用一个MyNumber类充当“生产者—消费者”模式的缓冲池实例
- Java producer-consumer(生产者/消费者模式)
- Java并发 生产者-消费者模式
- java中多线程通信实例:生产者消费者模式
- Java多线程-生产者消费者模式
- Java 并发编程(四)阻塞队列和生产者-消费者模式
- 生产者/消费者模式(阻塞队列) 一个类似于监听者模式的并发模型
- Java多线程——使用wait/notify实现生产者/消费者模式
- Java Note: 多线程的同步(互斥锁)的方法对比,信号量锁,读写锁的实现,生产者-消费者模式的实现
- java多线程之消费者生产者模式
- Java并发学习--生产者/消费者模式
- 老紫竹JAVA提高教程-信号量(Semaphore)在生产者和消费者模式的使用
- java消费者生产者模式及JDK之阻塞队列LinkedBlockingQueue实现
- Java线程(三):线程协作-生产者/消费者模式
- Java线程(学习整理)--4---一个简单的生产者、消费者模型
- Java中通过wait和notify来实现生产者和消费者模式
- Java多线程之生产者消费者模式
- JAVA 阻塞队列实现 生产者和消费者 模式
- 【设计模式】java生产者和消费者的代码实例
- Java多线程(十九):并发协作生产者消费者设计模式