All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anwen.mongo.incrementer.id.SystemClock Maven / Gradle / Ivy

There is a newer version: 2.1.6.1
Show newest version
package com.anwen.mongo.incrementer.id;

import java.sql.Timestamp;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author JiaChaoYang
 * 高并发场景下System.currentTimeMillis()的性能问题的优化
 * 

System.currentTimeMillis()的调用比new一个普通对象要耗时的多(具体耗时高出多少我还没测试过,有人说是100倍左右)

*

System.currentTimeMillis()之所以慢是因为去跟系统打了一次交道

*

后台定时更新时钟,JVM退出时,线程自动回收

*

10亿:43410,206,210.72815533980582%

*

1亿:4699,29,162.0344827586207%

*

1000万:480,12,40.0%

*

100万:50,10,5.0%

* @since 2023-02-13 16:14 **/ public class SystemClock { private final long period; private final AtomicLong now; private SystemClock(long period) { this.period = period; this.now = new AtomicLong(System.currentTimeMillis()); scheduleClockUpdating(); } private static SystemClock instance() { return InstanceHolder.INSTANCE; } public static long now() { return instance().currentTimeMillis(); } public static String nowDate() { return new Timestamp(instance().currentTimeMillis()).toString(); } private void scheduleClockUpdating() { ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> { Thread thread = new Thread(runnable, "System Clock"); thread.setDaemon(true); return thread; }); scheduler.scheduleAtFixedRate(() -> now.set(System.currentTimeMillis()), period, period, TimeUnit.MILLISECONDS); } private long currentTimeMillis() { return now.get(); } private static class InstanceHolder { public static final SystemClock INSTANCE = new SystemClock(1); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy