等待不活动的逻辑

时间:2016-04-08 11:10:39

标签: java multithreading thread-safety

我有一个TimeBomb对象。我必须根据时序逻辑使炸弹爆炸。炸弹将等待最多10秒,并且在该时间间隔内,如果该间隔中没有人将炸弹重置到最新时间,它将爆炸。我想出了一个实现。我通过将计时器设置为16:24:40的当前时间来启动等待逻辑。现在炸弹爆炸将在16:24:50爆炸。睡了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;
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

有几个选项可以使这个操作成为线程安全的

使用同步

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,则主线程必须等待访问timebomb对象。同样,如果主线程正在调用WaiterLogicresetBomb线程将等待访问timebomb对象。这可确保每个线程都会缓存bombTime数据的正确副本。

使用volatile关键字

volatile关键字使得线程永远不会缓存值。它总是直接记忆,基本上就像是在同步块中一样。

private volatile long bombTime;

使用原子对象

Wikipedia描述了一个原子对象:

  

在并发编程中,如果系统的其余部分在瞬间发生,则操作(或一组操作)是原子的,可线性化的,不可分割的或不可中断的。

在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);
    }
}

答案 1 :(得分:0)

使用wait(10000)并通知(在resetBomb方法中)而不是循环,它耗时较少。

相关问题