我们能像阿里巴巴/哨兵核心一样优化SystemLock吗

public class SystemClock { public long now() { return System.currentTimeMillis(); } }

public final class TimeUtil {

private static volatile long currentTimeMillis;

static {
    currentTimeMillis = System.currentTimeMillis();
    Thread daemon = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                currentTimeMillis = System.currentTimeMillis();
                try {
                    TimeUnit.MILLISECONDS.sleep(1);
                } catch (Throwable e) {

                }
            }
        }
    });
    daemon.setDaemon(true);
    daemon.setName("sentinel-time-tick-thread");
    daemon.start();
}

public static long currentTimeMillis() {
    return currentTimeMillis;
}

}

原提问者GitHub用户lazyfighter

展开
收起
芬奇福贵 2023-05-26 15:56:59 113 分享 版权
1 条回答
写回答
取消 提交回答
  • 到目前为止,RocketMQ的上述代码还没有遇到瓶颈。当然,上面的sentinel代码优化了系统调用的数量,但它也导致代码的复杂性上升。

    原回答者GitHub用户duhenglucky

    2023-05-26 17:53:22
    赞同 展开评论
问答地址: