Run a java function after a specific number of seconds
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Java provides a variety of ways to execute a function after a specific period of time. Whether you are building a simple application or a complex system, understanding how to schedule tasks is an essential skill. In this article, we’ll explore several techniques for achieving this, complete with technical explanations and examples.
Utilizing java.util.Timer
java.util.Timer and java.util.TimerTask provide a straightforward mechanism for scheduling tasks. You can use these classes to execute a task once after a delay or to execute repeatedly at a fixed rate.
Example
In the example above, a TimerTask is scheduled to run after a 5-second delay.
Key Points
- Timer and TimerTask: Facilitates scheduling but does not support complex models.
- Single-threaded execution: A single timer uses only one thread. If a task takes longer to execute, subsequent tasks may be delayed.
Employing ScheduledExecutorService
ScheduledExecutorService is a more flexible approach, especially useful when you need to handle concurrent tasks across multiple threads.
Example
- This code snippet creates a task that runs once after a 5-second delay.
- We employ
shutdown()to stop the executor after task execution.
Advantages
- Thread management: Allows the use of a thread pool.
- Advanced scheduling: Provides more advanced scheduling capabilities compared to
java.util.Timer.
Using java.lang.Thread.sleep
For simpler applications, you might use Thread.sleep, although it is not a scheduling tool per se.
Example
Drawbacks
- Blocking method: Pauses the executing thread, which can be inefficient.
- No task scheduling: Lacks the sophistication of scheduling tools.
Summary Table
| Feature | Timer and TimerTask | ScheduledExecutorService | Thread.sleep |
| Thread Control | Single-thread Timing issues | Multiple threads Better scalability | Blocks executing thread |
| Rescheduling & Repeating | Limited to manual re-scheduling Limited flexibility | Supports fixed-rate and delay Advanced features | Manual rescheduling only |
| Use Case | Simple periodic/single delays | Concurrent task execution Complex scheduling | Simple, non-concurrent tasks |
| Error Handling | Minimal error handling | More structured error management | Minimal error handling |
Conclusion
The choice of method to execute functions after a delay depends on the use case and complexity of your application. For basic needs, Timer and TimerTask could suffice. However, for concurrent and complex scheduling, ScheduledExecutorService is generally the better choice due to its flexibility and improved thread management. Thread.sleep, while easy to implement, should be reserved for very simple operations due to its blocking nature.
Consider threading implications, error handling, and task complexity when deciding which approach to implement for delayed task execution in Java.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.