How to implement a Timer class in Java? - java

Recently, I have been developing some android apps and I found that android.os.Handler class is very suitable for implementing a .NET Timer (By that I mean System.Windows.Forms.Timer and System.Timers.Timer).
If you don't know what a .NET timer is, it's a timer that can be stopped, started at any time and its interval can be changed any time.
So I did the following:
import android.os.Handler;
public class Timer {
private Handler handler;
private boolean paused;
private int interval;
private Runnable task = new Runnable () {
#Override
public void run() {
if (!paused) {
runnable.run ();
Timer.this.handler.postDelayed (this, interval);
}
}
};
private Runnable runnable;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public void startTimer () {
paused = false;
handler.postDelayed (task, interval);
}
public void stopTimer () {
paused = true;
}
public Timer (Runnable runnable, int interval, boolean started) {
handler = new Handler ();
this.runnable = runnable;
this.interval = interval;
if (started)
startTimer ();
}
}
And it came out ok. Also, this one runs on the UI thread which means that I can use this to change graphical stuff. (I mainly use timers for those stuff)
However, this only works for android though. If I want to make a "traditional" java program, I have to use the stuff in the JDK. So I tried the following:
import java.util.Timer;
import java.util.TimerTask;
public class DotNetTimer {
private Timer timer;
private boolean paused;
private int interval;
private TimerTask task = new TimerTask () {
#Override
public void run() {
if (!paused)
runnable.run();
}
};
public Runnable runnable;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
if (!paused) {
timer.cancel();
timer.scheduleAtFixedRate(task, interval, interval);
}
}
public void startTimer () {
timer.cancel();
timer.scheduleAtFixedRate(task, 0, interval);
}
public void stopTimer () {
paused = true;
}
public DotNetTimer (Runnable runnable, int interval, boolean started) {
timer = new Timer ();
this.runnable = runnable;
this.interval = interval;
if (started) {
paused = false;
startTimer ();
}
}
}
And I use this code to test it:
import static java.lang.System.out;
public class MyTestingClass {
static DotNetTimer timer;
public static void main(String[] args) {
Runnable r = new Runnable () {
int count = 0;
#Override
public void run() {
if (count < 5) {
count++;
out.println("Hello" + count);
} else {
timer.stopTimer();
}
}
};
timer = new DotNetTimer (r, 2000, true);
}
}
However, an IllegalStateException was thrown in the start timer method. I did some research on that and I found that java.util.Timer cannot be restarted after cancel(). And I know what you're saying, "why do you call cancel() in the startTimer() method?" If I don't call cancel(), the timer would have 2 tasks running when I call startTimer() when the timer is already started.
Any help will be appreciated.

From cancel() method in Timer class
Terminates this timer, discarding any currently scheduled tasks. Does
not interfere with a currently executing task (if it exists). Once a
timer has been terminated, its execution thread terminates gracefully,
and no more tasks may be scheduled on it.
Note that calling this method from within the run method of a timer
task that was invoked by this timer absolutely guarantees that the
ongoing task execution is the last task execution that will ever be
performed by this timer.
This method may be called repeatedly; the second and subsequent calls
have no effect.
so, internal thread of Timer is one-shot, you need to instantiate a new Timer object
You can check original source code of Timer class to understand (or replicate as you wish) how it really works
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Timer.java

I found out that there is a class in Android called Handler which can execute code with a delay. So I made use of this class to create a timer!
import android.os.Handler;
import android.support.annotation.NonNull;
import android.widget.TextView;
public class Timer implements Comparable<Timer> {
private Handler handler;
private boolean paused;
private TextView text;
private int minutes;
private int seconds;
private final Runnable timerTask = new Runnable () {
#Override
public void run() {
if (!paused) {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
}
text.setText (Timer.this.toString ());
Timer.this.handler.postDelayed (this, 1000);
}
}
};
#Override
public String toString () {
if (Integer.toString (seconds).length () == 1) {
return minutes + ":0" + seconds;
} else {
return minutes + ":" + seconds;
}
}
public void startTimer () {
paused = false;
handler.postDelayed (timerTask, 1000);
}
public void stopTimer () {
paused = true;
}
public void resetTimer () {
stopTimer ();
minutes = 0;
seconds = 0;
text.setText (toString ());
}
public Timer (TextView text) {
this.text = text;
handler = new Handler ();
}
public Timer (TextView text, String parseString) {
this (text);
String[] splitString = parseString.split (":");
minutes = Integer.parseInt (splitString[0]);
seconds = Integer.parseInt (splitString[1]);
}
#Override
public int compareTo(#NonNull Timer another) {
int numberOfSeconds = seconds + minutes * 60;
int anotherNumberOfSeconds = another.seconds + another.minutes * 60;
return ((Integer)numberOfSeconds).compareTo (anotherNumberOfSeconds);
}
}
And it has a really simple interface. Very easy to use.

Related

How to implemet isTimerRunning() for this class

I have this code, which basically provides a custom timer for my application.
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
public class SMSFirewallTimer {
private static Logger logger = Logger.getLogger(SMSFirewallTimer.class);
Timer timer;
String timerId;
public SMSFirewallTimer(int seconds, String string) {
timer = new Timer();
timerId = string;
logger.debug("SMSFirewallTimer: timer started for: "+seconds +"seconds");
timer.schedule(new Timeout(string), seconds*1000);
}
public void cancelTimer()
{
this.timer.cancel();
logger.debug("SMSFirewallTimer: Cancelling timer for : " + this.timerId);
}
class Timeout extends TimerTask {
private String string;
public Timeout(String string) {
this.string = string;
}
#Override
public void run() {
logger.debug("SMSFirewallTimer: Time's up! for: " + this.string );
timer.cancel(); //Terminate the timer thread
}
}
}
Now the problem arises that I want a isRunning() method in SMSFirewallTimer which will tell me that if the timer is still running or not, and it should also keep timeout in mind.
If I just declare a boolean as isTimer in this class it only holds good if we use cancelTimer(), but it wont come to know if timer expired, as its another scheduled method new Timeout(string).
Timeout is an inner class of SMSFirewallTimer which means that each instance of SMSFirewallTimer is tied to a specific instance of SMSFirewallTimer and that is has access to its fields and methods.
It should be sufficient to replace:
timer.cancel(); //Terminate the timer thread
with
cancelTimer(); //Terminate the timer thread
in the run method of Timeout.
Try this
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
public class SMSFirewallTimer {
private static Logger logger = Logger.getLogger(SMSFirewallTimer.class);
Timer timer;
String timerId;
private Boolean isRunning;
public SMSFirewallTimer(int seconds, String string) {
timer = new Timer();
timerId = string;
logger.debug("SMSFirewallTimer: timer started for: "+seconds +"seconds");
isRunning = true;
timer.schedule(new Timeout(string), seconds*1000);
}
public void cancelTimer()
{
isRunning = false;
this.timer.cancel();
logger.debug("SMSFirewallTimer: Cancelling timer for : " + this.timerId);
}
public boolean isRunning() {
return isRunning;
}
class Timeout extends TimerTask {
private String string;
public Timeout(String string) {
this.string = string;
}
#Override
public void run() {
logger.debug("SMSFirewallTimer: Time's up! for: " + this.string );
isRunning = false;
timer.cancel(); //Terminate the timer thread
}
}
}
Also you can keep reference for Timeout if you don't want to have access from your Timeout to your SMSFirewallTimer isRunning field.

How to display seconds of the timer in the output screen?

import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
public class Java {
Toolkit toolkit;
Timer timer;
int t=10000,total;
public Java(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
total =seconds * t;
System.out.println(total);
timer.schedule(new RemindTask(), total);
}
class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
toolkit.beep();
System.exit(0);
}
}
public static void main(String args[]) {
new Java(5);
System.out.println("Timer started");
}
}
How can I display the seconds similar to countdown timer in output screen, I want to use it in a quiz program
0:53 => 0:52 like this ...
What you can do is schedule a task at a fixed rate (Timer.scheduleAtFixedRate) using a period of 1 second. As far as possible, we should refrain from calling the System.exit(0) and wait for the threads to complete their tasks. We can make the main thread (the thread that started the timer) sleep for the duration of the timer tasks and then when it finally wakes up, it cancels the timer:
private final Timer timer;
public CountDownTimer(int seconds) {
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(seconds), 0, 1000);
}
class RemindTask extends TimerTask {
private volatile int remainingTimeInSeconds;
public RemindTask(int remainingTimeInSeconds) {
this.remainingTimeInSeconds = remainingTimeInSeconds;
}
public void run() {
if (remainingTimeInSeconds != 0) {
System.out.println(remainingTimeInSeconds + " ...");
remainingTimeInSeconds -= 1;
}
}
}
public static void main(String args[]) throws InterruptedException {
CountDownTimer t = new CountDownTimer(5);
System.out.println("Timer started");
Thread.sleep(5000);
t.end();
}
private void end() {
this.timer.cancel();
}

Handler.postDelayed causes lag

I created an application that can call using our SIP server. Now I want to do is to show the user how long his call has been on going. My script is actually working but I noticed that the longer the call, the laggy my app becomes. Here's the snippet of my code
Handler h = new Handler(Looper.getMainLooper());
_isOnCall = true;
long time = 0;
int x = 0;
while(_isOnCall) {
if (_isOnCall){
final int counter = 1 + x;
time += 1000;
h.postDelayed(new Runnable() {
#Override
public void run() {
(TargetDetailsActivity.this).runOnUiThread(new Runnable() {
#Override
public void run() {
final int seconds = counter % 60;
final int minutes = (counter % 3600) / 60;
final int hours = counter / 3600;
callcounter.post(new Runnable() {
#Override
public void run() {
callcounter.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
});
}
});
}
}, time);
x++;
}else{
break;
}
}
Basically what the code does is just to count the seconds/minutes/hour he's been on the phone. When he hangs up, I call the code below:
_isOnCall = false;
h.removeCallbacksAndMessages(null);
I'm not sure what causes the lag. Help! Thanks.
UPDATE
I was able to make this working by utilizing galvan's suggestion using Timer. Here's my code for future reference:
private Timer myTimer;
private int counter_time=0;
public void onCallEstablished() {
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
counter_time++;
int seconds = counter_time % 60;
int minutes = (counter_time % 3600) / 60;
int hours = counter_time / 3600;
callcounter.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
};
public void releaseCall(){
if(myTimer != null){
myTimer.cancel();
myTimer.purge();
counter_time = 0;
}
}
It looks like every Runnable saves a reference to his parent object, a nested Runnable in this case. Try to take an heap snapshot for detecting a memory issues to see if this is the case here.
You can also make a repeat task with time interval, and stop the loop when the call ends.
Timer timer = new Timer();
TimerTask myTask = new TimerTask() {
#Override
public void run() {
// whatever you need to do every 2 seconds
}
};
timer.scheduleAtFixedRate(myTask,
firstTime,
period);
for more info about timer you can take a look at the docs

android countdowntimer tick is not accurate

I am using a countdown timer for audio notification... and it's not accurate from the start...
using initial parameters
private final long startCountDown;
private final long intervalCountDown;
...
startCountDown = 180 * 1000; // 3 mns - to be set from Preferences later
intervalCountDown = 60 * 1000; // 1 mns - to be set from Preferences later
...
public void onTick(long millisUntilFinished) {
Log.d(TAG, "notify countDown: " + millisUntilFinished + " msecs");
}
countDownTimer = new SwimCountDownTimer(startCountDown,intervalCountDown);
....
public void startCountDown() {
Log.d(TAG, "start countDown for " + startCountDown + " msecs" );
countDownTimer.start();
}
I can see in the log that the initial countdown is correctly set to 180000 but the next one should be 120000 and it's set to 119945 !!!
04-27 14:50:42.146: I/SWIMMER(8670): notify countDown: 180000 msecs
04-27 14:51:42.206: I/SWIMMER(8670): notify countDown: 119945 msecs
This is quite annoying as the audio notifier is expecting to say only '2 minutes" and not "1 minute and fifty nine seconds" ...; why the interval is not right ... ?
I can tricj it in setting myself the text to speech string ... but is there any way to get correct data ?
thanks for suggestions
I know it's an old question- but I've also encountered the problem, and thought I would share my solution.
Apperantly CountDownTimer isn't very accurate, so I've decided to implement a more percise countdown timer, using java.util.Timer:
public abstract class PreciseCountdown extends Timer {
private long totalTime, interval, delay;
private TimerTask task;
private long startTime = -1;
private boolean restart = false, wasCancelled = false, wasStarted = false;
public PreciseCountdown(long totalTime, long interval) {
this(totalTime, interval, 0);
}
public PreciseCountdown(long totalTime, long interval, long delay) {
super("PreciseCountdown", true);
this.delay = delay;
this.interval = interval;
this.totalTime = totalTime;
this.task = getTask(totalTime);
}
public void start() {
wasStarted = true;
this.scheduleAtFixedRate(task, delay, interval);
}
public void restart() {
if(!wasStarted) {
start();
}
else if(wasCancelled) {
wasCancelled = false;
this.task = getTask(totalTime);
start();
}
else{
this.restart = true;
}
}
public void stop() {
this.wasCancelled = true;
this.task.cancel();
}
// Call this when there's no further use for this timer
public void dispose(){
cancel();
purge();
}
private TimerTask getTask(final long totalTime) {
return new TimerTask() {
#Override
public void run() {
long timeLeft;
if (startTime < 0 || restart) {
startTime = scheduledExecutionTime();
timeLeft = totalTime;
restart = false;
} else {
timeLeft = totalTime - (scheduledExecutionTime() - startTime);
if (timeLeft <= 0) {
this.cancel();
startTime = -1;
onFinished();
return;
}
}
onTick(timeLeft);
}
};
}
public abstract void onTick(long timeLeft);
public abstract void onFinished();
}
Usage example would be:
this.countDown = new PreciseCountdown(totalTime, interval, delay) {
#Override
public void onTick(long timeLeft) {
// update..
// note that this runs on a different thread, so to update any GUI components you need to use Activity.runOnUiThread()
}
#Override
public void onFinished() {
onTick(0); // when the timer finishes onTick isn't called
// count down is finished
}
};
to start the countdown, simply call countDown.start().
countDown.stop() stops the countDown, which could be restarted using countDown.restart().
Hope this is any help for anyone in the future.
This is an extension on what Noam Gal posted. I added extra functionality where you can pause and resume the timer. This was very helpful in my case.
public abstract class PreciseCountdownTimer extends Timer {
private long totalTime, interval, delay;
private TimerTask task;
private long startTime = -1;
private long timeLeft;
private boolean restart = false;
private boolean wasCancelled = false;
private boolean wasStarted = false;
public PreciseCountdownTimer(long totalTime, long interval) {
this(totalTime, interval, 0);
}
public PreciseCountdownTimer(long totalTime, long interval, long delay ) {
super("PreciseCountdownTimer", true);
this.delay = delay;
this.interval = interval;
this.totalTime = totalTime;
this.task = buildTask(totalTime);
}
private TimerTask buildTask(final long totalTime) {
return new TimerTask() {
#Override
public void run() {
if (startTime < 0 || restart) {
startTime = scheduledExecutionTime();
timeLeft = totalTime;
restart = false;
} else {
timeLeft = totalTime - (scheduledExecutionTime() - startTime);
if (timeLeft <= 0) {
this.cancel();
wasCancelled = true;
startTime = -1;
onFinished();
return;
}
}
onTick(timeLeft);
}
};
}
public void start() {
wasStarted = true;
this.scheduleAtFixedRate(task, delay, interval);
}
public void stop() {
this.wasCancelled = true;
this.task.cancel();
}
public void restart() {
if (!wasStarted) {
start();
} else if (wasCancelled) {
wasCancelled = false;
this.task = buildTask(totalTime);
start();
} else {
this.restart = true;
}
}
public void pause(){
wasCancelled = true;
this.task.cancel();
onPaused();
}
public void resume(){
wasCancelled = false;
this.task = buildTask(timeLeft);
this.startTime = - 1;
start();
onResumed();
}
// Call this when there's no further use for this timer
public void dispose() {
this.cancel();
this.purge();
}
public abstract void onTick(long timeLeft);
public abstract void onFinished();
public abstract void onPaused();
public abstract void onResumed();
}
Usage example would be almost exactly the same:
this.timer = new PreciseCountdownTimer(totalTime, interval, delay) {
#Override
public void onTick(long timeLeft) {
// note that this runs on a different thread, so to update any GUI components you need to use Activity.runOnUiThread()
}
#Override
public void onFinished() {
onTick(0); // when the timer finishes onTick isn't called
// count down is finished
}
#Override
public void onPaused() {
// runs after the timer has been paused
}
#Override
public void onResumed() {
// runs after the timer has been resumed
}
};
Enjoy and have fun :D
That's true and I observed the same behaviour (logging millisUntilFinished):
9999 // 1 ms lag
8997 // 3 ms lag
7995 // 5 ms lag
6993 // 7 ms lag
5991 // 9 ms lag
4987 // 13 ms lag
3985 // 15 ms lag
2979 // 21 ms lag
1975 // 25 ms lag
971 // 29 ms lag
The reason is that it's implementation doesn't take into account the time a message stays in thread's message queue and the time needed for synchronization.
I prepared the fixed version (repo, class source).
It prints the following sequence:
9999 // 1 ms lag
8999 // 1 ms lag
7999 // 1 ms lag
6997 // 3 ms lag
5997 // 3 ms lag
4998 // 2 ms lag
3997 // 3 ms lag
2998 // 2 ms lag
1997 // 3 ms lag
997 // 3 ms lag
Small lag is still here, but the most important thing is that it doesn't accumulate.
To install it add in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
implementation 'com.github.cardinalby:accurate-count-down-timer:1.0'
}
Instead of using millisUntilFinished, you can use a variable to hold the remaining time and in every onTick, minus the variable with the interval. In this way, remainingTime is always accurate.
private class MyTimer(
countDownTime: Long,
interval: Long
) : CountDownTimer(countDownTime, interval) {
private var remainingTime = countDownTime
override fun onFinish() {
}
override fun onTick(millisUntilFinished: Long) {
// consume remainingTime here and then minus interval
remainingTime -= interval
}
}

How can I change my TimerTask's execution period at runtime?I

How can I change period of Timer at runtime?
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// read new period
period = getPeriod();
doSomething();
}
}, 0, period);
You cannot do this directly, but you can cancel the tasks on the Timer and reschedule them with the desired period.
There is no getPeriod method.
You can do it like this:
private int period= 1000; // ms
private void startTimer() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// do something...
System.out.println("period = " + period);
period = 500; // change the period time
timer.cancel(); // cancel time
startTimer(); // start the time again with a new period time
}
}, 0, period);
}
You can use the following class to change the execution period of a TimerTask at runtime.
As already explained, it can not really change the period but has to cancel and reschedule the task:
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Supplier;
/**
* {#link TimerTask} with modifiable execution period.
*
* #author Datz
*/
public class EditablePeriodTimerTask extends TimerTask {
private Runnable task;
private Supplier<Long> period;
private Long oldP;
/**
* Constructor with task and supplier for period
*
* #param task the task to execute in {#link TimerTask#run()}
* #param period a provider for the period between task executions
*/
public EditablePeriodTimerTask(Runnable task, Supplier<Long> period) {
super();
Objects.requireNonNull(task);
Objects.requireNonNull(period);
this.task = task;
this.period = period;
}
private EditablePeriodTimerTask(Runnable task, Supplier<Long> period, Long oldP) {
this(task, period);
this.oldP = oldP;
}
public final void updateTimer() {
Long p = period.get();
Objects.requireNonNull(p);
if (oldP == null || !oldP.equals(p)) {
System.out.println(String.format("Period set to: %d s", p / 1000));
cancel();
new Timer().schedule(new EditablePeriodTimerTask(task, period, p), p, p);
// new Timer().scheduleAtFixedRate(new EditablePeriodTimerTask(task, period), p, p);
}
}
#Override
public void run() {
task.run();
updateTimer();
}
}
The Timer can bes started like this:
EditablePeriodTimerTask editableTimerTask =
new EditablePeriodTimerTask(runnable, () -> getPeriod());
editableTimerTask.updateTimer();
Where runnable is your real task to be executed and getPeriod() provides the period between the task executions. Which of course can change depending on your requirements.
Timer timer = new Timer(); //should not create timer again
private long periord = 1000; // periord is changed at runtime
public void runTaskPeriord() {
TimerTask task = new TimerTask() {
#Override
public void run() {
log.debug("Task run" );
if(periord <= 3000) {
this.cancel(); // cancel this task to run new task
periord += 1000;
runTaskPeriord();
}
}
};
timer.schedule(task, periord, periord);
int countDeletedTasks = timer.purge(); // remove cancel task from timer
}

Categories