How to call a method on specific time in java?

前端 未结 9 1835
Happy的楠姐
Happy的楠姐 2020-11-27 04:43

Is it possible to call a method in java at specific time? For example of I have a piece of code like this:

class Test{

    public static void main(String ar         


        
9条回答
  •  清酒与你
    2020-11-27 05:26

    If you are talking about SE then the Timer class might be what you are looking for http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html, available since Java 5.

    If you need to time something in an application server context I would recommend having a look at EJB timers http://www.javabeat.net/2007/03/ejb-3-0-timer-services-an-overview/ available since EJB 3.0.

    Alternatively, depending on what you are really trying to do, you could elaborate if using a cron job (or any other OS based timer method) would be more suitable, i.e. you don't want or can't have the VM running all the time.

提交回复
热议问题