java - 等待不活动的逻辑

标签 java multithreading thread-safety

我有一个定时炸弹对​​象。我必须根据时序逻辑让炸弹爆炸。炸弹将等待最多 10 秒,在该时间间隔内,如果没有人在该时间间隔内将炸弹重置为最晚时间,它将爆炸。我已经想出了一个实现方案。我通过将计时器设置为当前时间(例如 16:24:40)来启动等待逻辑。现在炸弹已准备好在 16:24:50 爆炸。 hibernate 2 秒后,我将炸弹重置为当前时间 16:24:42。现在炸弹应该只在 16:24:52 爆炸,因为最大等待时间是 10 秒。但我实现的逻辑总是在 16:24:50 爆炸。我哪里做错了?

public class TestTimeBomb {

    public static void main(String[] args) throws InterruptedException {
        TimeBomb bomb = new TimeBomb();
        long time1 = System.currentTimeMillis();
        System.out.println("First setting event at time " + getHumanReadableTime(time1));
        bomb.resetBomb(time1);
        Job job = new Job(bomb);
        new Thread(job).start();

        Thread.sleep(2000);

        long time2 = System.currentTimeMillis();
        System.out.println("Second setting event at time " + getHumanReadableTime(time2));
        bomb.resetBomb(time2);
    }

    public static Date getHumanReadableTime(long time) {
        Timestamp stamp = new Timestamp(time);
        Date date = new Date(stamp.getTime());
        return date;
    }
}

class Job implements Runnable {

    TimeBomb bomb;

    Job(TimeBomb bomb) {
        this.bomb = bomb;
    }

    @Override
    public void run() {
        WaiterLogic waiterLogic = new WaiterLogic(bomb);
        new Thread(waiterLogic).start();
    }
}

class WaiterLogic implements Runnable {
    private TimeBomb test;

    WaiterLogic(TimeBomb test) {
        this.test = test;
    }

    @Override
    public void run() {

        long currentTimeMillis = System.currentTimeMillis();
        System.out.println("Entering while loop this job should end at " + TestTimeBomb.getHumanReadableTime(currentTimeMillis + 10000));
        while (true) {
            long bombTime = test.getBombTime();
            long curentTime = System.currentTimeMillis();
            long diffTIme = curentTime - bombTime;
            if (diffTIme > 10000) 
                break;

        }
        long end = System.currentTimeMillis();
        System.out.println("This job should have ended at" + TestTimeBomb.getHumanReadableTime(test.getBombTime() + 10000));
        System.out.println("But ended at time " + TestTimeBomb.getHumanReadableTime(end));
        System.out.println("Diff is " + (end - (test.getBombTime() + 10000)));
    }

}

class TimeBomb {

    private long bombTime;

    public long getBombTime() {
        return bombTime;
    }

    public void resetBomb(long bombTime) {
        this.bombTime = bombTime;
    }
}

最佳答案

我认为这是一个并发问题,其中 bombTime 变量不是线程安全的。

在一个线程 (main) 上,您正在设置 bombTime 中的值,并在您正在读取的另一线程 (WaiterLogic) 中设置值值(value)。存在竞争条件,如果这两个线程尝试同时获取此数据,则每个线程中缓存的数据可能会变得不一致。

有几个选项可以使此操作线程安全

<小时/>

使用同步

synchronized 关键字用于让线程在执行任务时锁定对象。 synchronized 可以在 TimeBomb 类方法上使用,如下所示:

class TimeBomb {

    private long bombTime;

    public synchronized long getBombTime() {
        return bombTime;
    }

    public synchronized void resetBomb(long bombTime) {
        this.bombTime = bombTime;
    }
}

通过向每个方法添加 synchronized,如果 WaiterLogic 线程正在调用 getBombTime,主线程将不得不等待访问定时炸弹对​​象。同样,如果主线程正在调用 resetBomb,则 WaiterLogic 线程将等待访问定时炸弹对​​象。这可确保每个线程都会缓存 bombTime 数据的正确副本。

<小时/>

使用 volatile 关键字

volatile 关键字使得线程永远不会缓存值。它总是会直接进入内存,本质上就好像它在同步块(synchronized block)中一样。

private volatile long bombTime;
<小时/>

使用原子对象

Wikipedia描述一个原子对象:

In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously.

在java中,您可以使用java.util.concurrent.atomic.AtomicLong来达到此目的。该对象不需要 volatilesynchronized 关键字。这一切都由类本身处理。

class TimeBomb {

    private AtomicLong atomicBombTime;

    public long getBombTime() {
        return atomicBombTime.get();
    }

    public void resetBomb(long bombTime) {
        atomicBombTime.set(bombTime);
    }
}

关于java - 等待不活动的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36498144/

相关文章:

java - 关于Java生产者消费者解决方案的问题

Java 线程 Random.nextLong() 返回相同的数字

java - 在文本中查找电子邮件地址

java - 内部类。它的目的是什么?

multithreading - 4 个内核的 8 个逻辑线程并行运行速度最多可提高 4 倍?

python - 尽管主程序已完成,守护线程仍不退出

java - 使用 JDK 11+ 运行 JavaFX 应用程序

java - RabbitMq 管理问题

multithreading - 捕获异步 bash 命令的输出?

java - 查询线程执行情况