证明创建runnable实例和普通类时间一样长


import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//证明创建runnable实例和普通类时间一样长

public class Test002 {

private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
// private ArrayList<String> queue = new ArrayList<String>();
// private CyclicBarrier barrier = new CyclicBarrier(10000000);
private CountDownLatch latch = new CountDownLatch(100000);
ExecutorService es = Executors.newFixedThreadPool(4);

public static void main(String[] args) {
Test002 test001 = new Test002();
long timeStart = System.currentTimeMillis();
test001.begin();
System.out.println("##线程类实例化10000次时间:"+(System.currentTimeMillis()-timeStart));

timeStart = System.currentTimeMillis();
test001.begin2();
System.out.println("##普通类实例化10000次时间:"+(System.currentTimeMillis()-timeStart));

}

public void begin(){
for (int i = 0; i < 10000; i++) {
Runnable001 runnable001 = this.new Runnable001(i);
}
}
public void begin2(){
for (int j = 0; j < 10000; j++) {
Runnable002 Runnable002 = this.new Runnable002(j);
// new Thread(); // 时间主要花在创建线程上,使用runnable接口不占用时间
}
}

private class Runnable001 implements Runnable{
private int value;
public Runnable001(int value) {
this.value = value;
}

public void run() {

try {
// barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
queue.offer(value + "");
latch.countDown();//latch计数减一
}

}
private class Runnable002 {
private int value;
public Runnable002(int value) {
this.value = value;
}

public void run() {

try {
// barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
queue.offer(value + "");
latch.countDown();//latch计数减一
}

}
}

转载于:https://www.cnblogs.com/fengdaren/p/9765922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值