I am trying to write a java code that will give an alert at a specific time of day but I don’t know why I am failing(Please be gentle to my questions as I am totally new to programming). I have this code
new ScheduledThreadPoolExecutor(1).schedule(new Runnable() {
public void run() {
if(Calendar.SECOND==30)
{
JOptionPane.showMessageDialog(null, "Hola Amigo");
}
}
}, 1, TimeUnit.SECONDS);
Should I try to refresh the page? Please help…
You are checking that a fixed constant value
Calendar.SECOND(13) equals30. Obviously, this will never be true so the dialog will never appear. You need to check this field in aCalendarinstance.Also using
schedulemeans that the executor thread is only run once. UsescheduleAtFixedRate.Also you would need to call
showMessageDialogin theEDTto ensure that the call does not block theExecutorThread.If you want to call the
ExecutorServiceevery 30 seconds rather than repeatly checking the current second, you could call