Create a thread which never ends - java

I want to create a thread which never halts. Every second it will acquire the system time and display this on the console. This is what I have so far:
public class Test implements Runnable {
#Override
public void run() {
System.out.println(System.currentTimeMillis());
}
}
I'd like to avoid using a loop.

Using while(true) and TimeUnit.SECONDS.sleep is a possibility, but it is bad practice (as you can see from the sheer number of downvotes on this post). This SO answer gives some reasons as to why:
low level, subject to spurious wakeups
clock drift
control
intent of code
there are others.
The basic way to achieve this is to use a java.util.Timer, not to be confused with a javax.swing.Timer:
final Timer timer = new Timer("MyTimer");
timer.schedule(new TimerTask() {
#Override
public void run() {
System.out.println(System.currentTimeMillis());
}
}, 0, TimeUnit.SECONDS.toMillis(1));
You need to call timer.cancel() to stop the timer - as the timer is running a non-daemon thread your program will not exit until that is done.
A more advanced way, which allows multiple tasks to be scheduled to run at different intervals on a pool of the ScheduledExecutorService. This allows you to scheduleAtFixedRate which runs a task every second (regardless of how long it takes to run, i.e. the gap between start times is always the same) or scheduleWithFixedDelay which runs a task at one second intervals (i.e. the gap between the end of one run and the start of the next is always the same).
For example:
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
final ScheduledFuture<?> handle = executorService.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
System.out.println(System.currentTimeMillis());
}
}, 0, 1, TimeUnit.SECONDS);
To cancel the particular task you would call handle.cancel(false) (as interrupting has no effect) and to stop the executorService you would call executorService.shutdown() after which you might want to add a executorService.awaitTermination(1, TimeUnit.DAYS) to wait for all the tasks to finish.
EDIT
A comment This can be done more concisely in java 8 with lambda right? (not an expert at lambdas)
The first example, no. A Timer takes a TimerTask, this is an abstract class and not an #FunctionalInterface so a lambda is not possible. In the second case, sure:
final ScheduledFuture<?> handle = executorService.
scheduleAtFixedRate(() -> System.out.println(System.currentTimeMillis()), 0, 1, TimeUnit.SECONDS);

Related

Run a task continuously without delay inside a Java TimerTask

How do I run a specific set of instructions inside the TimerTask continuously without delay for a set amount of time ? Below are the codes I am attempting to implement the above.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("Test started at: " + new Date());
// Do something continuously without delay
System.out.println("Test finished at: " + new Date());
}
}, 0);
The second parameter to the schedule method is the time to begin the timer task (or delay relative to now), not the length of time that the timer will execute for.
It's not completely clear from your question but I'm assuming you want the task to start and stop at particular times (or delays relative to now) in the future. If so, the way I would approach this is to create a Thread that does the task you need. Since a TimerTask is a Runnable that is executed as a Thread once the Timer starts it, you can just use an instance of that TimerTask. Ensure that Runnable contains a settable field like running. In that Thread, run your task in a while loop like this:
public void run() {
while(running) { /* do my task */ }
}
Then, use one Timer to schedule the Runnable to start at the time you need. Use another Timer to set the running parameter of the same Thread to false at the time you want it to stop. The running parameter should be volatile to ensure that changes to it from the second timer Thread are seen by the first timer Thread immediately. So it would look something like this (not tested):
class StoppableTimerTask extends TimerTask {
private volatile boolean running = true;
public void stopRunning() { this.running = false; }
public void run() {
while(running) { /* do my task */ }
}
}
final StoppableTimerTask task = new StoppableTimerTask();
timer.schedule(task, startTime);
timer.schedule(new TimerTask() {
public void run() {
task.stopRunning();
}
}, stopTime);
Depending on what your "something" is, you may also want to look into Thread interrupts. For example, if it is doing blocking IO, your code won't loop and check the running value until the blocking IO completes. Interrupting the thread (may) cause that to happen. See http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#interrupt--. This may or may not work, and it can be tricky to get right, so if you need this Thread to exit as close to the desired time as possible, prefer running blocking I/O and similar operations with smaller timeouts so that the thread can check whether it should continue to run more often.
UPDATE: As per the comment indicating that the task should start right away, it becomes even simpler. The initial task doesn't even need to extend TimerTask -- it can just be a regular Thread that is started immediately. The timer is only needed to stop it at the specified future time.

ScheduledExecutorService does not end thread like Timer

I have 2 snippets of codes, one uses ScheduledExecutorService to execute a Thread at a certain time, the other uses Timer to execute a TimerTask at a certain time. The problem is while using ScheduledExecutorService, even though the thread's run() reaches the end, a monitor program indicates that it is still alive (see image bellow). The TimerTask, on the other hand, ends its thread once the execution is completed.
I have changed ScheduledExecutorService's Thread to TimerTask but received the same result. How do I resolve the issue while using ScheduledExecutorService?
Timer:
TimerTask task = new TimerTask()
{
#Override
public void run()
{
System.out.println("doing something");
}
};
Timer t = new Timer();
t.schedule(task, 250);
ScheduledExecutorService:
Thread task = new Thread()
{
#Override
public void run()
{
System.out.println("doing something");
}
};
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(task, 250, TimeUnit.MILLISECONDS);
In your second snippet, you've created a Thread instance but the ScheduledExecutorService has used the instance as a Runnable, ie. it only cares about its run method. It doesn't actually start the Thread.
ScheduledExecutorService maintains its own Thread(s) for executing tasks. With newSingleThreadScheduledExecutor, this is a single non-daemon Thread. You'll need to shutdown the ScheduledExecutorService for that Thread to complete.
The javadoc of Timer states
After the last live reference to a Timer object goes away and all
outstanding tasks have completed execution, the timer's task execution
thread terminates gracefully (and becomes subject to garbage
collection).
After it has executed your TimerTask, it will complete its internal thread.

How can I use time in Java to manipulate code?

I'm trying to test the use of time in Java to manipulate code. So let's say I have a app with an egg. The egg won't hatch until 60 seconds have passed in the application, what method or class would I use to do this?
The Timer class should do what you are after:
A facility for threads to schedule tasks for future execution in a background thread. Tasks
may be scheduled for one-time execution, or for repeated execution at
regular intervals.
You can take a look at a simple example available here.
You can use timer in a way like this
Timer timer = new Timer();
If you want your code to run multiple times:
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// Your logic here
// Your logic will run every 60 second
System.out.println("egg hatched");
}
}, 0, 60000);
If you want it to run only one time
timer.schedule(new TimerTask() {
#Override
public void run() {
// Your logic here
System.out.println("egg hatched");
}
}, 60000);
You can read more about class timer in java here
The easiest old-fashioned single thread approach is
Thread.sleep(60*1000);
System.out.println("egg hatched");
And there is no guaranty that it print exactly after minute
System.currentTimeMillis() returns the current time of the system in milliseconds to your. So you need to create a Thread checking for the current time in a while loop an react to it.
Try run it it a separate scheduled thread;
ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
Runnable hatcher = new Runnable() {
#Override
public void run() {
egg.hatch();
}
};
scheduler.schedule(hatcher, 60, TimeUnit.SECONDS);

Getting a TimerTask to run when using JUnit

I have a function that looks like this:
private Timer timer = new Timer();
private void doSomething() {
timer.schedule(new TimerTask() {
public void run() {
doSomethingElse();
}
},
(1000));
}
I'm trying to write JUnit tests for my code, and they are not behaving as expected when testing this code in particular. By using EclEmma, I'm able to see that my tests never touched the doSomethingElse() function.
How do I write tests in JUnit that will wait long enough for the TimerTask to finish before continuing with the test?
You can do something like this:
private Timer timer = new Timer();
private void doSomething() {
final CountDownLatch latch = new CountDownLatch(1);
timer.schedule(new TimerTask() {
public void run() {
doSomethingElse();
latch.countDown();
}
},
(1000));
latch.await();
// check results
}
The CountDownLatch#await() method will block the current thread until countDown() has been called at least the number of times specified at construction, in this case once. You can supply arguments to await() if you want to set a maximum amount of time to wait.
More information can be found here: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
JUnit is not really setup to run multithreaded code. When the main thread exits, it will stop all other Threads. In other words, if your thread scheduler doesn't context switch to the thread running the new TimerTask before returning from your #Test method, it will just kill that thread and exit the JVM, thus never executing your method.
You can either put a Thread.sleep(long) like Stefan suggested, or use a different design, possibly a ScheduledThreadPoolExecutor where you can awaitTermination with some timeout value.
You may use Thread.sleep(1000) in your test.

Multithreading concept

I am learning multi-threading concepts now. I can run a single thread with the help of handler and Runnable(). I want my code to run two threads, say Thread1 runs method1() and thread2 runs method2(). Thread1 should run for 2seconds and then sleep for 1second. In the mean time, thread2 should wakeup and run for 1second. Again, thread1 should run for 2seconds. This process should be done continuously. I am doing this in Android.
The question might look straight forward, but I have no other way, other than posting a question here, as I have gone through many tutorials and questions in this website. No post suits my context. Any suggestions will be appreciated. Thanks in advance.
You can do this by using ScheduledThreadPoolExecutor, with which you can achieve parallel execution of your tasks. A small sample example to schedule the tasks:
//creates a thread pool of size 2
int poolSize = 2;
// creates ScheduledThreadPoolExecutor object with number of thread 2
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(poolSize);
//starts executing after 1 second
ScheduledFuture<Callable-Type> sf = stpe.schedule(new TaskOne(), 1,TimeUnit.SECONDS);
//starts executing after 2 seconds
ScheduledFuture<Callable-Type> sf1 = stpe.schedule(new TaskTwo(), 2,TimeUnit.SECONDS);
And you can define your tasks as below:
class TaskOne implements Callable<Callable-Type> {
#Override
public Callable-Type call() throws Exception {
//DO YOUR WORK HERE
return callable-type;
}
}
class TaskTwo implements Callable<Callable-Type> {
#Override
public Callable-Type call() throws Exception {
//DO YOUR WORK HERE
return callable-type;
}
}
The advantages of using ScheduledThreadPoolExecutor over Timer are :
A Timer creates only a single thread for executing timer tasks. Scheduled
thread pools address this limitation by letting you provide multiple threads for executing deferred and periodic tasks.
Another problem with Timer is that it behaves poorly if a TimerTask throws an unchecked exception. The Timer thread doesn't catch the exception, so an unchecked exception thrown from a TimerTask terminates the timer thread.
Ref: Java Concurrency in Practice
Sample
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//your code
}
});
}
}, 2000, 1000); // 2000 is delay and 1000 is call period
There is also another schedule() methods that you can use to map your criteria.

Categories