Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a run() method of Timer of Java manually

Tags:

java

So, I set my timer:

timer.scheduleAtFixedRate(new TimerTask()
{
    @Override
    public void run()
    {
        ...
    }
}, 5, 5);

so I want to trigger this run() for testing something, not wait until it gets triggered. Is there a way?

like image 891
John Smith Avatar asked Sep 18 '25 01:09

John Smith


1 Answers

why using executors or threads? its not enough* to run method? :)

// define task 
TimerTask = tt new TimerTask()
{
    @Override
    public void run()
    {
        ...
    }
};


// schedule 
timer.scheduleAtFixedRate(tt,int,int);

// run as any other method :) 
tt.run();

*when run is not enought? when we can't or don't want to block the current thread!

like image 89
ceph3us Avatar answered Sep 20 '25 16:09

ceph3us