Reputation: 19
I'm working on a Java program that requires me to schedule a task to take place at regular intervals. But the restriction is I cannot use threads.
I have come across Timer and TimerTask, but I'm not sure whether they create threads or not..
Is there a way to accomplish this without using threads?
Thanks in advance.
Upvotes: 1
Views: 1842
Reputation: 338181
Thread.sleep
As the answer by Andreas says, you can sleep your current thread repeatedly to perform a recurring task. I understand your situation is a specify requirement in a homework class assignment to not use threads. I assume the intention of the assignment is to use no additional threads. Your executing code is always in a thread. Every Java app begins with an initial thread provided by the JVM.
But you should know that in real work that requirement of “no more threads” would be silly; the very purpose of threads is to carry out tasks on a different time frame. So, for real work I would recommend other more elegant, flexible, and powerful ways (search for Java Executors, and ScheduledExecutorService
).
But for your homework, the Thread
class does offer a static method sleep
for putting the current thread to sleep.
An Instant
is a moment on the timeline in UTC with a resolution of up to nanoseconds. Part of the java.time framework built into Java 8 and later. Back-ports available for Java 6 & 7 and for Android.
The TimeUnit
enum makes the conversion to milliseconds self-documenting.
Note that a sleeping thread may be awakened by another thread or by the JVM. When this happens, the awakened thread experiences an InterruptedException
.
long millisToSleep = java.util.concurrent.TimeUnit.MINUTES.toMillis ( 5 );
Instant now = Instant.now ();
Instant limit = now.plus ( 1 , ChronoUnit.HOURS );
while ( Instant.now ().isBefore ( limit ) ) {
System.out.println ( "Doing some stuff at " + Instant.now () );
// … do some stuff.
System.out.println ( "Going to sleep for " + millisToSleep + " milliseconds." );
try {
Thread.sleep ( millisToSleep ); // I do not recommend this approach, but it works. Better to use a 'ScheduledExecutorService'.
} catch ( InterruptedException ex ) {
System.out.println ( "This thread’s sleep was rudely interrupted at " + Instant.now () + ". Stopping any further work." );
break; // Bail out of the 'while' loop.
}
}
Upvotes: 0
Reputation: 167
You can try Quartz scheduler. Though it uses thread pool internally. It is stable and efficient.
Upvotes: 0
Reputation: 1562
It depends on your application design. A possible way is to design your application as a task to be executed. And launch it with Cron job (time-based job scheduler in Unix-like computer operating systems).
Upvotes: 0
Reputation: 159086
All Java timer, even the Swing Timer, uses a separate thread to wait and trigger the event at the appropriate time.
If you program has to do something at regular intervals, but cannot start new threads, then your main thread has to do it.
But, can it? What is it doing otherwise?
If the main thread is otherwise idle, you can calculate the amount of time until next event, and sleep until then.
Upvotes: 2