gpt4 book ai didi

java - 需要修复我的 Java 定时器代码

转载 作者:搜寻专家 更新时间:2023-11-01 03:25:21 24 4
gpt4 key购买 nike

任务 - 在一天中的指定时间打开和关闭灯泡。我需要知道如何根据下面给出的信息修复我的代码。我还需要知道我是否正确使用了计时器类,即我的代码设计是否正确?该代码可能有效,但它可能是糟糕的设计,以后会导致问题。我不希望发生这种情况。

输出是(这不是我真正想要的输出:( ) -

This is the main program
Current time is - xxx
Future time is - xxx+5sec
Future time is - xxx+10sec
Main program ends
Bulb B1 is OFF

期望的输出-

This is the main program
Current time is - xxx
Future time is - xxx+5sec
Future time is - xxx+10sec
Bulb B1 is ON //first on
Bulb B1 is OFF //then off
Main program ends//This should always be in the end.

如何修复下面的代码以获得我想要的内容?

灯泡

class Bulb {

private boolean state = false;//On or off
private String name;

Bulb(String name){

this.name = name;

}

public void setState(boolean state){

this.state = state;
if(this.state == true){

System.out.println("Bulb " + name + " is ON");

}else{

System.out.println("Bulb " + name + " is OFF");

}

}


public boolean getState(){
return this.state;

}


}

BulbJob 类是一个 TimerTask

import java.util.*;

class BulbJob extends TimerTask{

private Bulb bulbToHandle;
private boolean setBulbStateEqualTo;

BulbJob(Bulb toHandle){

this.bulbToHandle = toHandle;

}


//NOTE: Must be called before run(), otherwise default value is used
public void setBulbStateEqualTo(boolean setBulbStateEqualTo){

this.setBulbStateEqualTo = setBulbStateEqualTo;

}


//NOTE: call run() only before calling above method
public void run(){

this.bulbToHandle.setState(setBulbStateEqualTo);//Set on or off

}

}

BulbScheduler 类 - 这会安排灯泡何时打开或关闭。

import java.util.*;

@SuppressWarnings( "deprecation" )
class BulbScheduler {

public static void main(String args[]) throws InterruptedException{

System.out.println("This is the main program");

Timer time = new Timer();
Bulb b1 = new Bulb("B1");
BulbJob bj = new BulbJob(b1);

bj.setBulbStateEqualTo(true);//Task - Turn bulb on at time = afterCurrent

Date current = new Date();//Get current time and execute job ten seconds after this time
Date afterCurrent = (Date) current.clone();

System.out.println("Current time is - " + current);

int currentSecs = current.getSeconds();
int offset = 5;//number of seconds

afterCurrent.setSeconds(currentSecs + offset);
System.out.println("Future time is - " + afterCurrent);

time.schedule(bj, afterCurrent);//Schedule job "bj" at time = afterCurrent

//Now turn the bulb off at new time = newest afterTime
afterCurrent.setSeconds(currentSecs + 2 * offset);
System.out.println("Future time is - " + afterCurrent);

bj.setBulbStateEqualTo(false);//Task - Now turn the bulb off at time = afterCurrent

System.out.println("Main program ends");

}

}

最佳答案

本节:

time.schedule(bj, afterCurrent);//Schedule job "bj" at time = afterCurrent

//Now turn the bulb off at new time = newest afterTime
afterCurrent.setSeconds(currentSecs + 2 * offset);

只安排一个任务。如果您需要安排两次,请明确执行:

time.schedule(bj, afterCurrent);//Schedule job "bj" at time = afterCurrent

//Now turn the bulb off at new time = newest afterTime
afterCurrent.setSeconds(currentSecs + 2 * offset);
time.schedule(bj, afterCurrent);//Schedule job "bj" at time = afterCurrent

还有。这一行:

bj.setBulbStateEqualTo(false);

在主线程中执行,因此它将在两个 任务之前执行。您应该安排该语句在两个任务之间运行。

关于java - 需要修复我的 Java 定时器代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15333891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com