shellpool.java
来自「实现LRU算法的Cache源代码」· Java 代码 · 共 77 行
JAVA
77 行
package com.dw.system.cache ;
import java.io.* ;
import java.util.* ;
/**
* 根据测试,对于简单的类创建不适合用该池进行处理
*
*/
public class ShellPool
{
private Vector avail , busy ;
public ShellPool ()
{
avail = new Vector () ;
busy = new Vector () ;
}
public ShellPool (int initnum)
{
this () ;
for (int i = 0 ; i < initnum ; i ++)
avail.addElement (new Shell()) ;
}
//public abstract
public synchronized Shell getShell ()
{
if (!avail.isEmpty())
{
Shell exShell = (Shell)avail.lastElement () ;
int lastIndex = avail.size() - 1 ;
avail.removeElementAt (lastIndex) ;
busy.addElement (exShell) ;
return (exShell) ;
}
else
{
//if (total < max)
Shell newShell = new Shell () ;
busy.addElement (newShell) ;
return newShell ;
}
}
public synchronized void freeShell (Shell sh)
{
sh.clear () ;
busy.removeElement (sh) ;
avail.addElement (sh) ;
}
/*
public static void main (String[] args)
{
ShellPool sp = new ShellPool (10000) ;
int i ;
long s,e ;
Shell tmpsh ;
s = System.currentTimeMillis () ;
for (i = 0 ; i < 10000 ; i ++)
tmpsh = new Shell () ;
e = System.currentTimeMillis () ;
System.out.println ("Not use shell pool cost="+(e-s)) ;
//=0
s = System.currentTimeMillis () ;
for (i = 0 ; i < 10000 ; i ++)
tmpsh = sp.getShell () ;
e = System.currentTimeMillis () ;
System.out.println ("Using shell pool cost="+(e-s)) ;
//=20
}*/
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?