I think you have it right. Try 1000L
, as a long
value is expected, instead of an int
.
Edit: Tried it myself, and your code works for me:
package testdelay;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class TestDelay {
private static final ScheduledExecutorService existQueuePool = Executors.newScheduledThreadPool(1);
private static long start;
private static long end;
public static void main(String[] args) {
start = System.currentTimeMillis();
System.out.println("Before execution: " + start);
ScheduledFuture<?> mScheduledFuture = existQueuePool.schedule(new Runnable() {
@Override
public void run() {
end = System.currentTimeMillis();
System.out.println("Executed at: " + end);
System.out.println("Executed after delay of : " + (end - start) + " ms.");
}
}, 1000L, TimeUnit.MILLISECONDS);
}
}
Output:
Before execution: 1386350613147
Executed at: 1386350614149
Executed after delay of : 1002 ms.
It appears that it runs instantaneously because you log a message outside of the runnable, which is executed right after the submission to the service. You should log it in the runnable itself to see when it is ran.