0

I am new to this community and have a question about Java POO. It's about implementing a timer. The question is: why I cannot get a timer with a period of 1 ms. When I chose a period of e.g. 10 ms, everything works fine, but Java (or the computer?) looks like periods approaching 1 ms are ignored. The program (not really OOP oriented, sorry for that) should display a value every second. OK, I could use a delay of 1000 ms, but it must be also possible my way. I write another (graphics) application where the 1 ms period is important, but I chose this demo program to show to you. I my demo, a display is done only around every 10 seconds, but it should be 1 second.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class NewJFrame extends javax.swing.JFrame {

private int counter = 0;

public NewJFrame() {
    initComponents();
    timer.start();
}

Timer timer = new Timer(1, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        counter++;
        if (counter%1000==0)
           jLabel1.setText("ping");
    }
});

// <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(180, 180, 180)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(139, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(131, 131, 131)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(134, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

public static void main(String args[]) {
    
            new NewJFrame().setVisible(true);
            
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
// End of variables declaration                   
}

I expect the timer to work with a minimal period of 1 ms, as described in the docs.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
MaskedCat
  • 1
  • 1
  • 2
    Windows? Its timer usually has a default precision of about 15ms - probably related [Java Swing - Timer can't run faster than 15ms](https://stackoverflow.com/questions/71610514/java-swing-timer-cant-run-faster-than-16ms), [high resolution timer in java](https://stackoverflow.com/questions/13129538/high-resolution-timer-in-java), [Is it possible to make this Java timer have better than ~15 ms resolution?](https://stackoverflow.com/questions/25342872/is-it-possible-to-make-this-java-timer-have-better-than-15-ms-resolution) – user16320675 May 14 '23 at 11:25
  • 2
    This, `Timer timer = new Timer(1, ...)` is unreasonable. No timer can work at the 1 ms level. Consider changing this to a more realistic value. – Hovercraft Full Of Eels May 14 '23 at 11:29

0 Answers0