How to set a Timer in Java?

Viewed 657101

How to set a Timer, say for 2 minutes, to try to connect to a Database then throw exception if there is any issue in connection?

5 Answers

[Android] if someone looking to implement timer on android using java.

you need use UI thread like this to perform operations.

Timer timer = new Timer();
timer.schedule(new TimerTask() {
           @Override
            public void run() {
                ActivityName.this.runOnUiThread(new Runnable(){
                    @Override
                      public void run() {
                       // do something
                      }        
                });
            }
        }, 2000));
Related