I need to schedule a task to run after 2 minutes. Then when the time is up I need to check if we are still ONLINE
. If we are still online I simple don't do anything. If OFFLINE
then I will do some work.
private synchronized void schedule(ConnectionObj connectionObj)
{
if(connectionObj.getState() == ONLINE)
{
// schedule timer
}
else
{
// cancel task.
}
}
This is the code I am considering:
@Async
private synchronized void task(ConnectionObj connectionObj)
{
try
{
Thread.sleep(2000); // short time for test
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if(connectionObj.getState() == ONLINE)
{
// don't do anything
}
else
{
doWork();
}
}
For scheduling this task should I use @Async? I may still get many more calls to schedule while I am waiting inside the task()
method.
Does SpringBoot have something like a thread that I create each time schedule()
gets called so that this becomes easy?
I am looking for something similar to a postDelay()
from Android: how to use postDelayed() correctly in android studio?