为什么这段代码会占用这么多时钟时间?

时间:2018-07-31 03:19:31

标签: java swing awt

运行此代码几个小时后(打算连续运行几天),AWT-Windows线程开始使用7-14%的cpu,并且已使用的堆每5个字节上下浮动约30-40 mb -10秒,为什么只有运行好几个小时后才会发生这种情况? 下面是主要代码的运行片段:

    while(ses.getLoopSend()==true)
            {
                ses.setTimerState(true);
                if(ses.getIsIntervalChecked())
                {
                    mailm=new EmailCrmObj();
                    mailm.sendMailProc();
                }
                try {
                    for(j=0;j<ses.getInterval();j=j+500)
                    {
                    t.sleep(500);
                    //System.out.println(j);
                    if(ses.getTimerState()==false)
                        j=ses.getInterval();
                    ses.setProgress(j);
                    }

这是SendEmailScreen类

    public class SendEmailScreen extends JFrame {

private JPanel contentPane;
private boolean status,loopSend,sendOnClose,timerState;
private JFormattedTextField ftbSendInterval;
private JCheckBox chkSendInterval;
private JProgressBar progressBar;
private static JLabel lblCountDown;
private int progress,timeB4Send;

/**
 * Launch the application.

/**
 * Create the frame.
 */
public SendEmailScreen() {
    timerState=true;
    sendOnClose=true;
    status=false;
    loopSend=false;
    this.setTitle("Send Emails");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 204);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new GridLayout(3, 0, 0, 0));

    JPanel pnlLabels = new JPanel();
    contentPane.add(pnlLabels);
    pnlLabels.setLayout(new GridLayout(0, 1, 0, 0));

    JPanel panel_1 = new JPanel();
    pnlLabels.add(panel_1);
    panel_1.setLayout(new GridLayout(3, 1, 0, 0));

    JLabel lblChecksendAt = new JLabel("Uncheck 'Send at interval' to stop.");
    lblChecksendAt.setHorizontalAlignment(SwingConstants.LEFT);
    panel_1.add(lblChecksendAt);

    JLabel lblNewLabel = new JLabel("Enter time interval in hours and minutes.");
    lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
    panel_1.add(lblNewLabel);

    JLabel lblInDropDown = new JLabel("Click send emails to send at specified interval.");
    lblInDropDown.setHorizontalAlignment(SwingConstants.LEFT);
    panel_1.add(lblInDropDown);

    JPanel pnlValues = new JPanel();
    contentPane.add(pnlValues);
    pnlValues.setLayout(new GridLayout(0, 3, 0, 0));

    JPanel panel = new JPanel();
    FlowLayout flowLayout_1 = (FlowLayout) panel.getLayout();
    flowLayout_1.setAlignment(FlowLayout.LEFT);
    pnlValues.add(panel);

    chkSendInterval = new JCheckBox("Send at interval:");
    chkSendInterval.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            loopSend=false;
        }
    });
    panel.add(chkSendInterval);
    chkSendInterval.setSelected(true);
    JPanel panel_5 = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panel_5.getLayout();
    flowLayout.setAlignment(FlowLayout.LEFT);
    pnlValues.add(panel_5);
    Format form=new SimpleDateFormat("HH:mm");
    ftbSendInterval = new JFormattedTextField(form);
    ftbSendInterval.setColumns(6);
    ftbSendInterval.setText("01:00");
    ftbSendInterval.setEditable(true);
    panel_5.add(ftbSendInterval);

    JPanel panel_6 = new JPanel();
    pnlValues.add(panel_6);


    JLabel lblTimeTillNext = new JLabel("Will send in:");
    panel_6.add(lblTimeTillNext);

    lblCountDown = new JLabel("Awaiting input");
    panel_6.add(lblCountDown);

    JPanel panel_2 = new JPanel();
    contentPane.add(panel_2);
    panel_2.setLayout(new GridLayout(0, 3, 0, 0));

    JPanel panel_4 = new JPanel();
    FlowLayout flowLayout_3 = (FlowLayout) panel_4.getLayout();
    flowLayout_3.setAlignment(FlowLayout.LEFT);
    panel_2.add(panel_4);

    JButton btnSendEmails = new JButton("Send Emails");
    btnSendEmails.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                setlblCountDown("Sending Mail");
            if(chkSendInterval.isSelected()==true)
            {
                loopSend=true;
                timerState=false;
            }
            else 
            {
                sendOnClose=true;
                timerState=false;
                loopSend=false;
                status=true;
            }
        }
    });
    panel_4.add(btnSendEmails);

    JPanel panel_3 = new JPanel();
    panel_2.add(panel_3);

    JButton btnClose = new JButton("Return to Menu");
    btnClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            loopSend=false;
            sendOnClose=false;
            timerState=false;
            status=true;
        }
    });
    panel_3.add(btnClose);
}
    public boolean getStatus()
    {
        return status;
    }
    public boolean getLoopSend()
    {  
    return loopSend;
    }
    public int getInterval()
    {
        int interval;
        String[] HHmm=ftbSendInterval.getText().split(":");

  interval=Integer.parseInt(HHmm[0])*3600000+Integer.parseInt(HHmm[1])*60000;
        return interval;
    }
    public boolean getIsIntervalChecked()
    {
        return chkSendInterval.isSelected();
    }
    public void setProgress(int  d) {
int hh,mm,ss;
progress=d;
timeB4Send=getInterval()-progress;
hh=timeB4Send/3600000;
mm=timeB4Send%3600000/60000;
ss=timeB4Send%60000/1000;
//System.out.println(hh+":"+mm+"  "+timeB4Send);

if(getIsIntervalChecked())
{

    if(timeB4Send!=0)
        setlblCountDown(hh+" h, "+mm+" min, and "+ss+" sec");
    else 
        setlblCountDown("Sending Mail");
}
else
    setlblCountDown("Awaiting user input");
    }
    public boolean getSendMailOnClose() 
    {
        return sendOnClose;
     }
     public boolean getTimerState() 
     {
        return timerState;
     }
     public void setTimerState(boolean b)
     {
        timerState=b;
      }
     public void setlblCountDown(String s)
     {      
        lblCountDown.setText(s);
     }
   }

0 个答案:

没有答案