处理时间 java
Processing time in java
我正在尝试制作一个每 2 小时弹出消息的 java 应用程序,但出于测试目的,我已将时间缩短为 2 分钟。问题是,每当 while 条件被评估为 false 时,我都没有让 Jpanel 弹出。这是我正在使用的代码:
Calendar cal = Calendar.getInstance();
Calendar cal2;
SimpleDateFormat ST = new SimpleDateFormat("HHmm");
SimpleDateFormat CT;
String x = ST.format(cal.getTime());
int StartT = Integer.parseInt(x);
StartT= StartT+2;
int CurrentT;
//System.out.println( x2 );
do{
cal2 = Calendar.getInstance();
CT = new SimpleDateFormat("HHmm");
String y = ST.format(cal2.getTime());
CurrentT = Integer.parseInt(y);
}while(CurrentT < StartT );
JOptionPane.showMessageDialog(null, "MSG_Text..");
您听说过:
Thread#sleep(long millis)
Thread.sleep(2 * 60 * 1000)
这比主动等待要好得多(因为它消耗的 CPU 功率更少)。
如果您只需要在某些特殊时间执行,例如10:15:00.000,您可以使用
进行调整
long timeToWait = nextExecutionTIme - System.currentTimeMillis() + 1;
Thread.sleep(timeToWait);
编辑:
while (true) {
Thread.sleep(2 * 60 * 1000)
JOptionPane.showMessageDialog(null, "MSG_Text..");
}
不要重新发明轮子使用 Timer class 并以固定速率安排任务...
示例:
public static void main(String[] args){
Timer t = new Timer("testTimer", true);
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("hello world");
JOptionPane.showMessageDialog(null, "MSG_Text..");
}
}, 0L, TimeUnit.SECONDS.toMillis(30)); //run will be invoked every 30 seconds
while (true) {
}
}
您是否尝试过在 do-while 循环之后放置一个 System.out
,以查看您的代码是否完全完成了循环?
这个 Calendar / SimpleDateFormat / foo 东西真的很伤我的眼睛!
主动等待/忙等待。它会消耗很多 CPU 的电量。忙碌的等待是气候变化的主要原因!不要那样做! ( https://en.wikipedia.org/wiki/Busy_waiting )
使用Thread.sleep
。 1 行,更短、更容易、更不容易出错,几乎不消耗 CPU 功率。
Java 为几乎所有内容提供了一个 巨大的 框架。有用于睡眠/等待/时间计算等的 API。
不要自己构建。有很多人设计 JDK,检查代码是否有错误,使其易于使用。
我认为你应该使用比你自己的函数更好的 Timertask
当它达到2359时也不会再执行
一天的最后一分钟。
我正在尝试制作一个每 2 小时弹出消息的 java 应用程序,但出于测试目的,我已将时间缩短为 2 分钟。问题是,每当 while 条件被评估为 false 时,我都没有让 Jpanel 弹出。这是我正在使用的代码:
Calendar cal = Calendar.getInstance();
Calendar cal2;
SimpleDateFormat ST = new SimpleDateFormat("HHmm");
SimpleDateFormat CT;
String x = ST.format(cal.getTime());
int StartT = Integer.parseInt(x);
StartT= StartT+2;
int CurrentT;
//System.out.println( x2 );
do{
cal2 = Calendar.getInstance();
CT = new SimpleDateFormat("HHmm");
String y = ST.format(cal2.getTime());
CurrentT = Integer.parseInt(y);
}while(CurrentT < StartT );
JOptionPane.showMessageDialog(null, "MSG_Text..");
您听说过: Thread#sleep(long millis)
Thread.sleep(2 * 60 * 1000)
这比主动等待要好得多(因为它消耗的 CPU 功率更少)。
如果您只需要在某些特殊时间执行,例如10:15:00.000,您可以使用
进行调整long timeToWait = nextExecutionTIme - System.currentTimeMillis() + 1;
Thread.sleep(timeToWait);
编辑:
while (true) {
Thread.sleep(2 * 60 * 1000)
JOptionPane.showMessageDialog(null, "MSG_Text..");
}
不要重新发明轮子使用 Timer class 并以固定速率安排任务...
示例:
public static void main(String[] args){
Timer t = new Timer("testTimer", true);
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("hello world");
JOptionPane.showMessageDialog(null, "MSG_Text..");
}
}, 0L, TimeUnit.SECONDS.toMillis(30)); //run will be invoked every 30 seconds
while (true) {
}
}
您是否尝试过在 do-while 循环之后放置一个 System.out
,以查看您的代码是否完全完成了循环?
这个 Calendar / SimpleDateFormat / foo 东西真的很伤我的眼睛!
主动等待/忙等待。它会消耗很多 CPU 的电量。忙碌的等待是气候变化的主要原因!不要那样做! ( https://en.wikipedia.org/wiki/Busy_waiting )
使用
Thread.sleep
。 1 行,更短、更容易、更不容易出错,几乎不消耗 CPU 功率。
Java 为几乎所有内容提供了一个 巨大的 框架。有用于睡眠/等待/时间计算等的 API。
不要自己构建。有很多人设计 JDK,检查代码是否有错误,使其易于使用。
我认为你应该使用比你自己的函数更好的 Timertask 当它达到2359时也不会再执行 一天的最后一分钟。