Program doesn't output anything while there is a Timer and an ActionEvent, any ideas?

190 views Asked by At

This is supposed to print out the current time every second

import javax.swing.Timer;

public class Timer1{
    final static int SPEED = 1000;
public static void main(String[] args){
    Timer t = new Timer(SPEED, new TimerListener());
    t.start();
}
}

import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class TimerListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
    System.out.println(new Date()); 
}
}

The above are the 2 classes in my program

2

There are 2 answers

4
Freek de Bruijn On BEST ANSWER

I think you do not see any output because the program terminates before the timer gets a chance. If you add a delay at the end, you will see date/time lines.

In the code, I have also added an example with the java.util.Timer that alex2410 suggested:

public class TimerTest {
    private static final int SPEED = 1000;

    public static void main(final String[] args) {
        new TimerTest().testTimers();
    }

    private void testTimers() {
        testSwingTimer();
        System.out.println("------");
        testUtilTimer();
    }

    private void testSwingTimer() {
        final javax.swing.Timer t =
            new javax.swing.Timer(SPEED, e -> System.out.println("testSwingTimer: "
                                                                 + new Date()));
        t.start();
        delay(4000);
    }

    private void testUtilTimer() {
        final TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("testUtilTimer: " + new Date());
            }
        };
        final java.util.Timer timer = new java.util.Timer(true);
        timer.schedule(task, 0, SPEED);
        delay(2000);
    }

    private void delay(final int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }
}
2
Sainath S.R On

Ironically i just faced the same issue too , see below(just to illustrate the point ,may not always be suitable)

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JOptionPane;
import javax.swing.Timer;

public class TimerTest {



    public static void main(String[] args) {
        int time=2000;//10 sec
        Timer t1=new Timer(time, new TimerListener());
        t1.start();
        JOptionPane.showConfirmDialog(null, "Click To stop Timer");
    }

}

class TimerListener implements ActionListener{

     private int count=5;
    @Override
    public void actionPerformed(ActionEvent e) {
        Date d=new Date();
        System.out.println(d);
        Toolkit t=Toolkit.getDefaultToolkit();
        t.beep();

    }

    }

The issue is as mentioned by others ,the main method starts the Timer and immediately executes the next line of code and so on and exits eventually before the Timer can fire , so you need to keep main running , the example above does this by showing a GUI Dialog window , until you click on it the Timer event keeps on firing after every interval.

Alternatively if you want to run the application from a Terminal (terminal mode as in without GUI ,Eg:Linux Terminal Emulations etc) , another approach(rather bad ) is shown below.(I would recommend not using this )

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.Timer;

public class TimerTest {



    public static void main(String[] args) {
        int time=2000;//10 sec
        Timer t1=new Timer(time, new TimerListener());
        t1.start();

        while(true){

        }
    }

}

class TimerListener implements ActionListener{

     private int count=5;
    @Override
    public void actionPerformed(ActionEvent e) {
        Date d=new Date();
        System.out.println(d);
        Toolkit t=Toolkit.getDefaultToolkit();
        t.beep();
        if(getCount()>0)
        {
            count--;
        }
        else
            System.out.printf("Done");

    }
    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

}