Java 计时器消耗 CPU
Java Timer Consumes CPU
//I call the methods under the init
{
showdate();
showtime();
}
void showdate()
{
Date d = new Date();
SimpleDateFormat a = new SimpleDateFormat("YYYY-MM-dd");
date.setText(a.format(d));
}
void showtime()
{
new Timer(0, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Date d = new Date();
SimpleDateFormat a = new SimpleDateFormat("hh:mm:ss");
time.setText(a.format(d));
}
}).start();
}
这段代码占用了我 80% 的 CPU,我真的需要在我的表格上显示时间和日期。
延迟时间为 0 是不现实的,除了燃烧 CPU 个周期外没有任何用处。尝试更实际的值,如 13 或 15,如果程序功能正常,甚至更长。
//I call the methods under the init
{
showdate();
showtime();
}
void showdate()
{
Date d = new Date();
SimpleDateFormat a = new SimpleDateFormat("YYYY-MM-dd");
date.setText(a.format(d));
}
void showtime()
{
new Timer(0, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Date d = new Date();
SimpleDateFormat a = new SimpleDateFormat("hh:mm:ss");
time.setText(a.format(d));
}
}).start();
}
这段代码占用了我 80% 的 CPU,我真的需要在我的表格上显示时间和日期。
延迟时间为 0 是不现实的,除了燃烧 CPU 个周期外没有任何用处。尝试更实际的值,如 13 或 15,如果程序功能正常,甚至更长。