package a;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class ExampleThread implements Runnable{
int id=0;
@Override
public void run() {
id++;
}
}
public class Sender {
public static void main(String[] args) {
Runtime run=Runtime.getRuntime(); //创建runtime 对象
run.gc(); //清理垃圾
long freeMomery= run.freeMemory();
long currentTime=System.currentTimeMillis();
for (int i = 0; i <10000 ; i++) {
ExampleThread a= new ExampleThread();
new Thread(a).start();
}
System.out.println("10000并发不使用连接池内存消耗(字节):"+(freeMomery-run.freeMemory()));
System.out.println("10000并发不使用连接池时间消耗(毫秒):"+(System.currentTimeMillis()-currentTime));
run.gc();
freeMomery= run.freeMemory();
currentTime=System.currentTimeMillis();
ExecutorService pool= Executors.newFixedThreadPool(1);
for (int i = 0; i < 10000; i++) {
pool.submit(new ExampleThread());
}
System.out.println("10000并发使用连接池时间消耗(毫秒):"+(System.currentTimeMillis()-currentTime));
System.out.println("10000并发使用连接池内存消耗(字节):"+(freeMomery-run.freeMemory()));
}
}
打印结果:
10000并发不使用连接池内存消耗(字节):3046704
10000并发不使用连接池时间消耗(毫秒):774
10000并发使用连接池时间消耗(毫秒):18
10000并发使用连接池内存消耗(字节):1363272