使用日期时效率低下的Java程序 [英] Inefficient Java program when using date

查看:108
本文介绍了使用日期时效率低下的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序似乎正在使用20%的CPU和大约1GB的RAM。我认为它是因为我循环的日期。我试图让我的JFrame上出现一个时钟(小时,分钟和秒钟总是更新)。我的问题是,如何使我的程序不那么饿?
这是我的代码:

  while(true){
Date date = new Date();
time.setText(date.getHours()+hours+ date.getMinutes()
+分钟+ date.getSeconds()+seconds!);
}


解决方案

不循环。无论应用程序如何,上述无限循环将对资源持续需求。



在这种情况下,您似乎正在使用Swing。这对于 Swing 应用程序来说更为糟糕。无限循环阻止UI更新。



使用 Swing Timer ,并设置一个足够长的时间间隔,允许观察更新,并减少CPU的开销。 1000 毫秒应该做。

  public class TimerDemo {

public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame框架= new JFrame(Timer Demo);
final JLabel timeLabel =
new JLabel(------------------------ -----------------------);
定时器定时器=新的Timer(1000,新的ActionListener(){

SimpleDateFormat format = new SimpleDateFormat(HHhours'mm'minutes'ss'seconds');
@Override
public void actionPerformed(ActionEvent e){
Date date = new Date ();
timeLabel.setText(format.format(date));
}
});

frame.setDef aultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(timeLabel);
frame.setVisible(true);
frame.pack();
timer.start();
}
});
}
}


My program seem to be using 20% of the CPU and around 1GB of RAM. I think its because I am looping the date. I am trying to make a clock appear on my JFrame (hours, mins and seconds always updating). My question is, how can I make my program less hungry for power? Here's my code:

while(true){
    Date date = new Date();
    time.setText(date.getHours() + " hours " + date.getMinutes() 
                 + " minutes " + date.getSeconds() + " seconds!");
}

解决方案

Don't loop. Whatever the application, the above infinite loop will place a constant demand on resources.

In this case, it appears you are using Swing. This is even worse for Swing applications. Infinite loops prevent UI updates.

Use a Swing Timer instead and set an period interval large enough that will allow updates to be observed and will demand less overhead from the CPU. 1000 milliseconds should do.

public class TimerDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Timer Demo");
                final JLabel timeLabel = 
                             new JLabel("-----------------------------------------------");
                Timer timer = new Timer(1000, new ActionListener() {

                    SimpleDateFormat format = new SimpleDateFormat("HH' hours 'mm' minutes 'ss' seconds'");
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Date date = new Date();
                        timeLabel.setText(format.format(date));
                    }
                });

                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.add(timeLabel);
                frame.setVisible(true);
                frame.pack();
                timer.start();
            }
        });
    }
}

这篇关于使用日期时效率低下的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆