????????????????????java.util.LinkedList??java.util.ArrayList??get(intindex)?????????ArrayList???LinkedList??Ч??????????????????????????????????

?????????????????????

publicinterfaceFoo{

publicvoidtestArrayList();

publicvoidtestLinkedList();

}

??????????????????????????????

publicclassFooImplimplementsFoo{

 

privateListlink=newLinkedList();

privateListarray=newArrayList();

 

publicFooImpl()

{

for(inti=0;i<10000;i++)

{

array.add(newInteger(i));

link.add(newInteger(i));

}

}

 

publicvoidtestArrayList()

{

for(inti=0;i<10000;i++)

array.get(i);

}

 

publicvoidtestLinkedList()

{

for(inti=0;i<10000;i++)

link.get(i);

}

}

??????????????????????????????InvocationHandler???

importjava.lang.reflect.InvocationHandler;

importjava.lang.reflect.Method;

importjava.lang.reflect.*;

publicclassHandlerimplementsInvocationHandler{

privateObjectobj;

publicHandler(Objectobj){

this.obj=obj;

}

publicstaticObjectnewInstance(Objectobj){

Objectresult=Proxy.newProxyInstance(obj.getClass().getClassLoader()??

obj.getClass().getInterfaces()??newHandler(obj));

return(result);

}

publicObjectinvoke(Objectproxy??Methodmethod??Object[]args)throwsThrowable{

Objectresult;

try{

System.out.print("beginmethod"+method.getName()+"(");

for(inti=0;args!=null&&i<args.length;i++){

if(i>0)System.out.print("??");

System.out.print(""+

args[i].toString());

}

System.out.println(")");

longstart=System.currentTimeMillis();

result=method.invoke(obj??args);

longend=System.currentTimeMillis();

System.out.println("themethod"+method.getName()+"lasts"+(end-start)+"ms");

}catch(InvocationTargetExceptione){

throwe.getTargetException();

}catch(Exceptione){

thrownewRuntimeException

("unexpectedinvocationexception:"+

e.getMessage());

}finally{

System.out.println("endmethod"+method.getName());

}

returnresult;

}

}