Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: timer Uses a lot of hardware resources

  1. #1

    Post timer Uses a lot of hardware resources

    HI
    why wen i use timer for show time in jlabel Uses a lot of hardware resources? Approximately 35% of the CPU (cor i7)and 170MG of ram
    How can I reduce this amount after using the timer?
    this is my code
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.Timer;
     
    public class NewJFrame extends javax.swing.JFrame {
     
     
        public NewJFrame() {
            initComponents();
            time();
        }
     
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jLabel1 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jLabel1.setText("jLabel1");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(182, 182, 182)
                    .addComponent(jLabel1)
                    .addContainerGap(184, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(138, 138, 138)
                    .addComponent(jLabel1)
                    .addContainerGap(148, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        public void time() {
            new Timer(0, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Date d = new Date();
                    SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
                    jLabel1.setText(s.format(d));                 //time= jlabel 
                }
            }).start();
        }
     
        public static void main(String args[]) {
     
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JLabel jLabel1;
        // End of variables declaration                   
    }
    Last edited by cnmeysam; April 5th, 2021 at 05:18 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,068
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: timer Uses a lot of hardware resources

    Why is the delay for the timer set at 0 milliseconds?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3

    Default Re: timer Uses a lot of hardware resources

    i just need show clock in label like digital clock
    I have just started programming and I have not passed any academic course. I do not know exactly how to code and what values to use.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,068
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: timer Uses a lot of hardware resources

    The time display in your code shows seconds as the smallest unit of time. Changing the display continuously with no delay makes no sense.
    The delay could be 500 ms and the change in the displayed seconds would be as fast as a human could detect.
    Try that value and see if you can detect any error in the display.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 5th, 2021)

  6. #5

    Default Re: timer Uses a lot of hardware resources

    It is not visually visible, but does it cause disturbance in real time?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,068
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: timer Uses a lot of hardware resources

    does it cause disturbance in real time?
    Sorry, I do not understand. Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 5th, 2021)

  9. #7

    Default Re: timer Uses a lot of hardware resources

    I mean, when I set 500 milliseconds, time does not lag behind, I do not mean in the show
    But also in reality
    For example, if it is 12 o'clock in the OS, after 30 minutes, my program should show 12:30, not 12:29.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,068
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: timer Uses a lot of hardware resources

    The time comes from the Date object. The Timer control how frequently the time is displayed. Change the delay to 5000 ms and see if the time is correctly displayed every 5 seconds.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 5th, 2021)

Similar Threads

  1. Using resources on Android?
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: August 18th, 2012, 12:08 PM
  2. new to hardware programming
    By kenmurimi in forum Java Theory & Questions
    Replies: 3
    Last Post: August 16th, 2012, 06:53 AM
  3. How to set resources folder?
    By A1Artem in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: July 7th, 2012, 09:45 PM
  4. hardware requirement for Java
    By mandroid in forum Java Theory & Questions
    Replies: 5
    Last Post: June 7th, 2011, 12:52 PM
  5. hardware ID
    By ttsdinesh in forum Java Native Interface
    Replies: 7
    Last Post: September 27th, 2009, 03:17 AM