System.currentTimeMillis()真的再多线程下面会有性能问题吗

360 阅读2分钟

测试代码,先创建线程再执行代码

` public class TimeDemo {

public static Integer systemCore=Runtime.getRuntime().availableProcessors();
public static void main(String[] args) throws InterruptedException {
    int size=10000000;
    int coreSize=300;
    CountDownLatch countDownLatch=new CountDownLatch(coreSize);
    Thread[] thread = getThread(coreSize, countDownLatch, size);
    CountDownLatch countDownLatch1=new CountDownLatch(coreSize);
    Thread[] thread1 = getThread(coreSize, countDownLatch1, size);
    singleThread(size);
    multiThread(size,thread,countDownLatch);
    multiThreadCache(size,thread1,countDownLatch1);

}
public static void singleThread(Integer size){
    long start = System.nanoTime();
    for (int i = 0; i < size; i++) {
        System.currentTimeMillis();
    }
    long ns = System.nanoTime() - start;
    System.out.println("单线程,数量:"+size+";耗时:"+ns+"毫秒:"+(ns/1000000));
}
public static void multiThread(Integer size,Thread[] threads,CountDownLatch countDownLatch) throws InterruptedException {
    long start = System.nanoTime();
    for (Thread thread : threads) {
        thread.start();
    }
    countDownLatch.await();
    long ns = System.nanoTime() - start;
    System.out.println("电脑核心数:"+systemCore+";线程数:"+threads.length+";每个线程数量:"+size+";耗时:"+ns+";毫秒:"+(ns/1000000));
}
public static void multiThreadCache(Integer size,Thread[] threads,CountDownLatch countDownLatch) throws InterruptedException {
    long start = System.nanoTime();
    for (Thread thread : threads) {
        thread.start();
    }
    countDownLatch.await();
    long ns = System.nanoTime() - start;
    System.out.println("电脑核心数:"+systemCore+";工具类,线程数:"+threads.length+";每个线程数量:"+size+";耗时:"+ns+";毫秒:"+(ns/1000000));
}
public static Thread[] getThread(Integer threadSize,CountDownLatch countDownLatch,Integer size){
    Thread[] threads=new Thread[threadSize];
    for (int i = 0; i < threadSize; i++) {
        MyRunnable myRunnable = new MyRunnable(countDownLatch, size);
        threads[i]=new Thread(myRunnable);
    }
    return threads;
}
static class MyRunnable implements  Runnable {
    private CountDownLatch countDownLatch;
    private Integer size;

    public MyRunnable(CountDownLatch countDownLatch, Integer size) {
        this.countDownLatch = countDownLatch;
        this.size = size;
    }

    @Override
    public void run() {
        for (int i = 0; i < size; i++) {
            System.currentTimeMillis();
        }
        countDownLatch.countDown();
    }
}
static class MyRunnableCache implements  Runnable {
    private CountDownLatch countDownLatch;
    private Integer size;

    public MyRunnableCache(CountDownLatch countDownLatch, Integer size) {
        this.countDownLatch = countDownLatch;
        this.size = size;
    }

    @Override
    public void run() {
        for (int i = 0; i < size; i++) {
            CacheMillisecondClock.getInstance().getTime();
        }
        countDownLatch.countDown();
    }
}

} `

缓存工具类

` public class CacheMillisecondClock {

private static volatile long time=0L;
private static long interval=1000;
private volatile static CacheMillisecondClock cacheMillisecondClock;
private CacheMillisecondClock() {
    startCacheClock();
}

private void startCacheClock() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            time=System.currentTimeMillis();
            try {
                TimeUnit.MILLISECONDS.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public static CacheMillisecondClock getInstance(){
    if(cacheMillisecondClock==null){
        synchronized (CacheMillisecondClock.class){
            if(cacheMillisecondClock==null){
                cacheMillisecondClock=new CacheMillisecondClock();
            }
        }
    }
    return  cacheMillisecondClock;
}

public long getTime() {
    return time;
}

}

` spring boot tomcat默认线程数是200,我就用200个线程测试

windows下(200线程,6个核心)

image.png

测试线程数200,每个线程数10000000,一共执行了200*10000000=2000000000;由于电脑只有6个核心, 并行6个线程,也就是每个行程比单线程数据实际是200/6=33.33倍,耗时1395/37=37.70倍,差距不明显, 事实证明是没有性能问题的,缓存工具类是1181/37=31.91倍 windows下(300线程,6个核心)

image.png

这个缓存工具类跟直接调用的差距更小,实际证明性能差距微乎其微,

我再来测试下linux系统是否有性能问题 linux下(200线程,4个核心)

image.png

这个性能差距也很微小,实际证明不适用缓存工具类也可以再高并发的情况下正常使用