1.對象的強、軟、弱和虛引用
ReferenceQueue queue =new ReferenceQueue ();
PhantomReference pr =new PhantomReference (object,queue);
|
MyObject aRef =new MyObject();
SoftReference aSoftRef=new SoftReference(aRef);
|
aRef = null;
|
MyObject anotherRef=(MyObject)aSoftRef.get();
|
ReferenceQueue queue =new ReferenceQueue();
SoftReference ref=new SoftReference(aMyObject,queue);
|
SoftReference ref = null;
while ((ref = (EmployeeRef)q.poll()) !=
null) {
//清除ref
}
|
publicclass Employee {
private Stringid;//僱員的標識號碼
private Stringname;//僱員姓名
private Stringdepartment;//該僱員所在部門
private StringPhone;//該僱員聯繫電話
privateintsalary;//該僱員薪資
private Stringorigin;//該僱員信息的來源
//構造方法
public Employee(String id) {
this.id = id;
getDataFromlnfoCenter();
}
//到數據庫中取得僱員信息
privatevoid getDataFromlnfoCenter() {
//和數據庫建立連接井查詢該僱員的信息,將查詢結果賦值
//給name,department,plone,salary等變量
//同時將origin賦值爲"From DataBase"
}
……
|
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.Hashtable;
publicclass EmployeeCache {
staticprivate EmployeeCachecache;//一個Cache實例
private Hashtable<String,EmployeeRef>employeeRefs;//用於Chche內容的存儲
private ReferenceQueue<Employee>q;//垃圾Reference的隊列
//繼承SoftReference,使得每一個實例都具有可識別的標識。
//並且該標識與其在HashMap內的key相同。
privateclass EmployeeRefextends
SoftReference<Employee> {
private String_key =
"";
public EmployeeRef(Employee em, ReferenceQueue<Employee> q) {
super(em, q);
_key = em.getID();
}
}
//構建一個緩存器實例
private EmployeeCache() {
employeeRefs =new Hashtable<String,EmployeeRef>();
q =new ReferenceQueue<Employee>();
}
//取得緩存器實例
publicstatic EmployeeCache getInstance() {
if (cache ==null)
{
cache =new EmployeeCache();
}
returncache;
}
//以軟引用的方式對一個Employee對象的實例進行引用並保存該引用
privatevoid cacheEmployee(Employee em) {
cleanCache();//清除垃圾引用
EmployeeRef ref =
new EmployeeRef(em,q);
employeeRefs.put(em.getID(), ref);
}
//依據所指定的ID號,重新獲取相應Employee對象的實例
public Employee getEmployee(String ID) {
Employee em = null;
//緩存中是否有該Employee實例的軟引用,如果有,從軟引用中取得。
if (employeeRefs.containsKey(ID)) {
EmployeeRef ref = (EmployeeRef)employeeRefs.get(ID);
em = (Employee) ref.get();
}
//如果沒有軟引用,或者從軟引用中得到的實例是null,重新構建一個實例,
//並保存對這個新建實例的軟引用
if (em ==null) {
em = new Employee(ID);
System.out.println("Retrieve From EmployeeInfoCenter. ID=" +
ID);
this.cacheEmployee(em);
}
return em;
}
//清除那些所軟引用的Employee對象已經被回收的EmployeeRef對象
privatevoid cleanCache() {
EmployeeRef ref =
null;
while ((ref = (EmployeeRef)q.poll()) !=
null) {
employeeRefs.remove(ref._key);
}
}
//清除Cache內的全部內容
publicvoid clearCache() {
cleanCache();
employeeRefs.clear();
System.gc();
System.runFinalization();
}
}
|
publicclass SocketManager {
private Map<Socket, User>m =
new HashMap<Socket, User>();
publicvoid setUser(Socket s, User u) {
m.put(s, u);
}
public User getUser(Socket s) {
returnm.get(s);
}
publicvoid removeUser(Socket s) {
m.remove(s);
}
}
|
import java.util.WeakHashMap;
class Element {
private Stringident;
public Element(String id) {
ident = id;
}
public String toString() {
returnident;
}
publicint hashCode() {
returnident.hashCode();
}
publicboolean equals(Object obj) {
return objinstanceof Element &&ident.equals(((Element)
obj).ident);
}
protectedvoid finalize(){
System.out.println("Finalizing "+getClass().getSimpleName()+"
"+ident);
}
}
class Keyextends Element{
public Key(String id){
super(id);
}
}
class Valueextends Element{
public Value (String id){
super(id);
}
}
publicclass CanonicalMapping {
publicstaticvoid main(String[] args){
int size=1000;
Key[] keys=new Key[size];
WeakHashMap<Key,Value> map=new WeakHashMap<Key,Value>();
for(int i=0;i<size;i++){
Key k=new Key(Integer.toString(i));
Value v=new Value(Integer.toString(i));
if(i%3==0)
keys[i]=k;
map.put(k, v);
}
System.gc();
}
}
|
publicclass SocketManager {
private Map<Socket,User>m =
new WeakHashMap<Socket,User>();
publicvoid setUser(Socket s, User u) {
m.put(s, u);
}
public User getUser(Socket s) {
returnm.get(s);
}
}
|