Java缓存种的实现

Java缓存类的实现
package com.jxsafe.source.common.applications.source.classinfo.collection;

import java.util.LinkedList;

import com.jxsafe.source.common.applications.source.classinfo.collection.imp.IReLinkedList;

/**
* 链表操作 (缓存管理)
* @author CaoXiang
* @version 1.0
*/
@SuppressWarnings("serial")
public class ReLinkedList extends LinkedList implements IReLinkedList{

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReLinkedList#addLastElement(java.lang.Object)
  */
@SuppressWarnings("unchecked")
public void addLastElement(Object obj) {
  addLast(obj);
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReLinkedList#removeFirstElement()
  */
public Object removeFirstElement() {
  return removeFirst();
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReLinkedList#isEmpty()
  */
public boolean isEmpty() {
  return super.isEmpty();
}

}

====================================

package com.jxsafe.source.common.applications.source.classinfo.collection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jxsafe.source.common.applications.source.classinfo.collection.imp.IReMap;
import com.jxsafe.source.common.applications.source.classinfo.thread.DestroySource;
import com.jxsafe.source.common.applications.source.classinfo.thread.LocalThreads;
import com.jxsafe.source.common.applications.source.classinfo.thread.RemoteThread;
import com.jxsafe.source.common.applications.source.classinfo.thread.imp.ILocalThreads;

import net.sf.ehcache.CacheException;

/**
* Map封装内的实现 (数据缓存)
* @author CaoXiang
* @version 1.0
*/
@SuppressWarnings("serial")
public class ReMap implements IReMap {

/**
  * 所有个人基本信息的缓存 调整缓存大小
  */
@SuppressWarnings("unused")
private static final HashMap MESSAGE_CACHE = new HashMap<String, List<Object>>(
   256);

/**
  * 链表 保存所有Map缓存的CODE编码
  */
ReLinkedList MESSAGE_CACHE_LIST = new ReLinkedList();

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReMap#put(java.lang.Object, java.lang.Object)
  */
@SuppressWarnings("unchecked")
public void put(Object key, Object value) throws CacheException {
  MESSAGE_CACHE.put(String.valueOf(key), value);
  MESSAGE_CACHE_LIST.addFirst(MESSAGE_CACHE);
  ILocalThreads localThreads = new LocalThreads();
  RemoteThread remoteThread = new RemoteThread(localThreads, MESSAGE_CACHE_LIST.getLast());
  DestroySource destroySource = new DestroySource(localThreads, MESSAGE_CACHE_LIST.getLast());
  remoteThread.start();
  destroySource.start();
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReMap#get(java.lang.Object)
  */
public Object get(Object key) throws CacheException {
  return MESSAGE_CACHE.get(String.valueOf(key));
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReMap#remove(java.lang.Object)
  */
public void remove(Object key) {
  MESSAGE_CACHE.remove(String.valueOf(key));
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.collection.IReMap#isEmpty(java.util.Map)
  */
public boolean isEmpty(Map map) {
  return map.isEmpty();
}

/**
     * 清空所有的值
     */
    public void clear() {
     MESSAGE_CACHE.clear();
    }

}

====================================

package com.jxsafe.source.common.applications.source.classinfo.thread;

import com.jxsafe.source.common.applications.source.classinfo.log.SafeLog;
import com.jxsafe.source.common.applications.source.classinfo.thread.imp.ILocalThread;

/**
* 本地线程类的实现 (测试用)
* @author CaoXiang
* @version 1.0
*/
public class LocalThread extends Thread implements Runnable, ILocalThread{

/**
  * 线程对象
  */
Thread localThread = new Thread();

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.ILocalThread#startThread(java.lang.String)
  */
public void startThread(String className) {
  localThread = new Thread(className);
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.ILocalThread#run()
  */
@SuppressWarnings("static-access")
public void run() {
  //SafeLog.safeSystem("线程已经启动");
  for(int i=2; i>0; i--) {
   SafeLog.safeSystem("线程已经启动"+",到记时: "+i);
   try {
    this.localThread.sleep(1000);
   } catch (InterruptedException e) {
   }
  }
  for(int i=3;i>0;i--) {
   try {
    this.localThread.sleep(1000);
   } catch (InterruptedException e) {
   }
   SafeLog.safeSystem("正在重新构建应用程序..."+i);
  }
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.ILocalThread#sleepThread(int)
  */
@SuppressWarnings("static-access")
public void sleepThread(int sleepNumber) throws InterruptedException {
  this.localThread.sleep(sleepNumber);
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.ILocalThread#stopThread()
  */
@SuppressWarnings({"deprecation","static-access"})
public void stopThread() throws InterruptedException {
  if(localThread.interrupted()) {
   localThread.stop();
   SafeLog.safeSystem("线程已经停止...");
  } else {
   this.sleepThread(1000);
  }
}

public static void main(String[] args) {
  LocalThread l = new LocalThread();
  gcThread g2 = new gcThread();
  Thread g1 = new Thread(g2);
  Thread g3 = new Thread(l);
  g1.start();
  g3.start();
 
}

}

/**
* 垃圾回收类
* @author CaoXiang
*/
class gcThread extends Thread implements Runnable {

/**
  * 需要释放资源的类
  */
Class className = null;

/**
  * 状态标识
  */
boolean flg = true;

/**
  * 通过构造函数传递需要释放的类
  * @param className
  */
gcThread() {
}

/**
  * 运行方法
  */
public void run() {
  this.cleanUp();
  SafeLog.safeSystem("缓存清理完毕...");
}

/**
  * 停止运行
  */
public void stopRun() {
  flg = false;
}

/**
  * 清除内存中的数据
  */
public void cleanUp() {
  System.gc();
}

}

====================================

package com.jxsafe.source.common.applications.source.classinfo.thread;

import com.jxsafe.source.common.applications.source.classinfo.thread.imp.ILocalThreads;
import com.jxsafe.source.common.applications.source.classinfo.thread.imp.IRemoteThread;

/**
* 远程线程类的实现
* @author CaoXiang
* @version 1.0
*/
public class RemoteThread extends Thread implements IRemoteThread {

/**
  * 远程类线程对象
  */
private ILocalThreads localThread;

/**
  * 需要释放资源的对象
  */
private Object object;

/**
  * 构造函数初始化
  * @param c
  * @param object
  */
public RemoteThread(ILocalThreads c, Object object) {
  localThread = c;
  this.object = object;
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.IRemoteThread#run()
  */
public void run() {
   localThread.put(object);
   try {
    sleep((int) (Math.random() * 100));
   } catch (InterruptedException e) {
   }
}

}

====================================

package com.jxsafe.source.common.applications.source.classinfo.thread;

import com.jxsafe.source.common.applications.source.classinfo.thread.imp.ILocalThreads;

/**
* 线程中枢类
* @author CaoXiang
*
*/
public class LocalThreads implements ILocalThreads {

/**
  * 需要释放资源的对象
  */
private Object object;

/**
  * 判断是释放还是存储
  */
private boolean available = false;

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.ILocalThreads#get()
  */
public synchronized Object get() {
  while (available == false) {
   try {
    //沉睡
    wait();
   } catch (InterruptedException e) {
   }
  }
  available = false;
  //唤醒
  notifyAll();
  return object;
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.ILocalThreads#put(java.lang.Object)
  */
public synchronized void put(Object object) {
  while (available == true) {
   try {
    //沉睡
    wait();
   } catch (InterruptedException e) {
   }
  }
  this.object = object;
  available = true;
  //唤醒
  notifyAll();
}

}

====================================
package com.jxsafe.source.common.applications.source.classinfo.thread;

import com.jxsafe.source.common.applications.source.classinfo.thread.imp.IDestroySource;
import com.jxsafe.source.common.applications.source.classinfo.thread.imp.ILocalThreads;

/**
* 销毁线程类定义 (资源回收)
* @author CaoXiang
*/
public class DestroySource extends Thread implements IDestroySource {

/**
  * 释放资源的对象类实例
  */
private ILocalThreads localThread;

/**
  * 需要释放资源的类对象
  */
@SuppressWarnings("unused")
private Object object;

/**
  * 构造函数初始化设置
  * @param c
  * @param object
  */
public DestroySource(ILocalThreads c, Object object) {
  localThread = c;
  this.object = object;
}

/* (non-Javadoc)
  * @see com.jxsafe.source.common.applications.source.classinfo.thread.IDestroySource#run()
  */
public void run() {
  @SuppressWarnings("unused")
  Object value;
  value = localThread.get();  
  value = null;
  System.gc();
  System.runFinalization();
  System.gc();
}

}