如何使用长加法器执行

uwopmtnx  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(440)

您好,我有一个应用程序计算时间并根据它引发一个事件。我想让我的应用程序线程安全使用长加法器或任何合适的。
我的班级在下面;

@Autowired
    EventListenerConfiguration eventListenerConfiguration;

    private volatile long lastReceivedMessage = System.currentTimeMillis();

    public void consume(String message) Integer partition,
                        (Headers.OFFSET) Long offset, Acknowledgment ack) {

        lastReceivedMessage = System.currentTimeMillis();

        try {
            seervice.processMessage(message, ack, null);

        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
    }

    @Scheduled(fixedDelayString = "${listenScheduled}", initialDelay = 100000)
    private void distanceBetweenLastReceivedMessageAndCurrentTime() {

        long currentTime = System.currentTimeMillis() - lastReceivedMessage;

        if (currentTime >= EventListenerConfiguration .getTotalMilliSecondTimeForError()) {

            EventUtil.publishEvent(THROW_ERROR_EVENT, EventSeverityStatus.ERROR, EventTypeStatus.CUSTOM, null);

        } else (currentTime >= EventListenerConfiguration.getTotalMilliSecondTimeForWarn()) {

            EventUtil.publishEvent(THROW_WARN_EVENT, EventSeverityStatus.WARN, EventTypeStatus.CUSTOM, null);

        }
}

因此,基本上如何转换我的代码,而不改变太多的长加法器,并执行currenttime lastreceivemessage
非常感谢。

4dbbbstv

4dbbbstv1#

晚上好看来你的问题主要是 lastReceivedMessage 是由两个线程使用的受保护资源。无论运行什么,consume方法都会生成它,然后spring会生成它 @scheduled 线程消耗它。
添加volatile关键字不会阻止代码读取字段。它只会阻止代码缓存变量(通读易变的https://www.geeksforgeeks.org/volatile-keyword-in-java/)如果要将@scheduled块视为关键区域,并阻止更新 lastRecievedMessage 在完成之前,我建议如下:

private volatile long lastReceivedMessage = System.currentTimeMillis();
    private Semaphore resourceLock = new Semaphore(1);

    public void consume(String message) Integer partition,
    (Headers.OFFSET) Long offset, Acknowledgment ack) {

        resourceLock.acquireUninterruptibly();
        try {
            lastReceivedMessage = System.currentTimeMillis();
        } finally {
            resourceLock.release();
        }

        try {
            seervice.processMessage(message, ack, null);

        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
    }

    @Scheduled(fixedDelayString = "${listenScheduled}", initialDelay = 100000)
    private void distanceBetweenLastReceivedMessageAndCurrentTime() {
        long currentTime = 0;

        resourceLock.acquireUninterruptibly();
        try {
                currentTime = System.currentTimeMillis() - lastReceivedMessage;

            if (currentTime >= EventListenerConfiguration .getTotalMilliSecondTimeForError()) {
                EventUtil.publishEvent(THROW_ERROR_EVENT, EventSeverityStatus.ERROR, EventTypeStatus.CUSTOM, null);

            } else (currentTime >= EventListenerConfiguration.getTotalMilliSecondTimeForWarn()) {
                EventUtil.publishEvent(THROW_WARN_EVENT, EventSeverityStatus.WARN, EventTypeStatus.CUSTOM, null);
            }
        } finally {
            resourceLock.release();
        }
    }

相关问题