Java 等待不活动逻辑
我有一个定时炸弹。我必须根据定时逻辑使炸弹爆炸。炸弹将最多等待10秒,在该时间间隔内,如果没有人在该时间间隔内重置炸弹至最晚爆炸时间。我已经提出了一个实现方案。我通过将计时器设置为当前时间(如16:24:40)来启动等待逻辑。现在炸弹将在16:24:50爆炸。睡了2秒钟后,我将炸弹重置为当前时间16:24:42。现在炸弹应该只在16:24:52爆炸,因为最大等待时间是10秒。但是我实现的逻辑总是在16:24:50爆炸。我哪里做错了Java 等待不活动逻辑,java,multithreading,thread-safety,Java,Multithreading,Thread Safety,我有一个定时炸弹。我必须根据定时逻辑使炸弹爆炸。炸弹将最多等待10秒,在该时间间隔内,如果没有人在该时间间隔内重置炸弹至最晚爆炸时间。我已经提出了一个实现方案。我通过将计时器设置为当前时间(如16:24:40)来启动等待逻辑。现在炸弹将在16:24:50爆炸。睡了2秒钟后,我将炸弹重置为当前时间16:24:42。现在炸弹应该只在16:24:52爆炸,因为最大等待时间是10秒。但是我实现的逻辑总是在16:24:50爆炸。我哪里做错了 public class TestTimeBomb {
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
)中,您正在读取值。存在一种竞争条件,如果这两个线程同时尝试获取此数据,则每个线程中缓存的数据可能会变得不一致
有几个选项可以使此操作线程安全
使用同步
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
关键字使线程从不缓存值。它总是直接进入内存,本质上就像它在同步块中一样
private volatile long bombTime;
使用原子对象 描述原子对象: 在并发编程中,一个操作(或一组操作)是原子的、可线性化的、不可分割的或不可中断的,如果系统的其余部分认为它是瞬时发生的 在java中,您可以为此使用
java.util.concurrent.AtomicLong
。此对象不需要volatile
或synchronized
关键字。它全部由类本身处理
class TimeBomb {
private AtomicLong atomicBombTime;
public long getBombTime() {
return atomicBombTime.get();
}
public void resetBomb(long bombTime) {
atomicBombTime.set(bombTime);
}
}
使用wait(10000)和notify(在resetBomb方法中)代替循环,耗时更少。我认为它在2016年4月8日星期五17:29:11的第一次设置事件中起到了预期的作用,而该循环作业应在2016年4月8日星期五17:29:21 IST结束。2016年4月8日星期五17:29:13 IST的第二次设置事件该作业应在2016年4月8日星期五17:29:23 IST结束,但在2016年4月8日星期五17:29:21 IST结束2016年差异为-2028这是我在2016年4月8日星期五17:32:01 IST进入同一程序时得到的
第一次设置事件,而此作业应在2016年4月8日星期五17:32:12 IST结束。2016年4月8日星期五17:32:04 IST第二次设置事件此作业应在2016年4月8日星期五17:32:14 IST结束,但在2016年4月8日星期五结束17:32:14 IST 2016差异为1
No@Sanjeev。while循环比预期提前2秒结束。我多次运行了您的代码,它按照预期工作,始终以diff=1毫秒结束