Is there a code to check if a timer is running? - java

I have a game where I am scheduling a timer. I have this CoresManager file:
package com.rs.cores;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public final class CoresManager {
protected static volatile boolean shutdown;
public static WorldThread worldThread;
public static ExecutorService serverWorkerChannelExecutor;
public static ExecutorService serverBossChannelExecutor;
public static Timer fastExecutor;
public static ScheduledExecutorService slowExecutor;
public static int serverWorkersCount;
public static void init() {
worldThread = new WorldThread();
int availableProcessors = Runtime.getRuntime().availableProcessors();
serverWorkersCount = availableProcessors >= 6 ? availableProcessors - (availableProcessors >= 12 ? 7 : 5) : 1;
serverWorkerChannelExecutor = availableProcessors >= 6 ? Executors
.newFixedThreadPool(availableProcessors - (availableProcessors >= 12 ? 7 : 5),
new DecoderThreadFactory()) : Executors.newSingleThreadExecutor(new DecoderThreadFactory());
serverBossChannelExecutor = Executors
.newSingleThreadExecutor(new DecoderThreadFactory());
fastExecutor = new Timer("Fast Executor");
slowExecutor = availableProcessors >= 6 ? Executors.newScheduledThreadPool(availableProcessors >= 12 ? 4 : 2,
new SlowThreadFactory()) : Executors
.newSingleThreadScheduledExecutor(new SlowThreadFactory());
worldThread.start();
}
public static void shutdown() {
serverWorkerChannelExecutor.shutdown();
serverBossChannelExecutor.shutdown();
fastExecutor.cancel();
slowExecutor.shutdown();
shutdown = true;
}
private CoresManager() {
}
}
I am using this inside the game:
private void startTimer() {
CoresManager.fastExecutor.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (timer == 0 || timer < 1) {
player.sm("Your timer has ended! The NPCs will no longer spawn.");
timer = 0;
this.cancel();
exitInstance();
return;
}
timer--;
timerchecker = true;
seconds = timer % 60;
player.setTimer(timer);
minutes = TimeUnit.SECONDS.toMinutes(timer);
}
}, 0, 1000);
}
The CoresManager Timer stops running if the player logs out AND the server gets rebooted. To make it run again, I added a code to make it do startTimer() again once you log back in. However, since the timer still runs if the server didn't log out, the timer starts running twice. The Timer starts getting subtracted by 2, or more, depending on how many times you log out and in. I figure that it would fix if there was a code to determine if the timer is already running. Is there a way to do this? Please help!

I don't see anything in the documentation that provides for checking the status on a TimerTask object (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html) so one option would be to extend TimerTask and create your own class. Instead of using an anonymous TimerTask, you could create something along the lines of:
public class CoresTimerTask extends TimerTask {
private boolean hasStarted = false;
#Overrides
public void run() {
this.hasStarted = true;
//rest of run logic here...
}
public boolean hasRunStarted() {
return this.hasStarted;
}
}
and just maintain a reference to this CoresTimerTask object, which you then pass into startTimer(). You can then later check this object via hasRunStarted.

public long scheduledExecutionTime()
Returns the scheduled execution time of the most recent actual execution of this task. (If this method is invoked while task execution is in progress, the return value is the scheduled execution time of the ongoing task The return value is undefined if the task has yet to commence its first execution.
This method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.
first thing periodically running tasks need set/reset state flag
second (when i look at examples) it is better to seal this type of class
but if someone insist to have such methods
public abstract class NonInterruptableTask extends TimerTask {
protected boolean isDone = false;
public boolean isDone() {return isDone;}
protected abstract void doTaskWork();
#Override
public void run() {
isDone = false;
doTaskWork();
isDone = true;
}
}
usage:
TimerTask myTask = new NonInterruptableTask() {
#Override
public void doTaskWork() {
//job here
}
};

you could also declare a boolean state called like "timerstate" or whatever and make it by default to be false. whenever you start a timer you could change this boolean to true and you'd be able to keep track of the timer.
public boolean timerstate;
public Timer t1;
// some code goes here to do whatever you want
if(timerstate == true) {
t1.cancel();
t1.purge();
t1 = new Timer();
} else{
t1.schedule(new TimerTask() {
#Override
public void run() {
timerstate = true;
//rest of code for the timer goes here
}
}
}

Related

Reseting a timer if particular condition is met?

I have an event listener which detects when the mouse is being moved in a certain pane of my program. From this, I want to be able to perform some action if the mouse stays idle for too long.
I have looked all over earlier today, to try and find an explanation and example which details how to start, stop/cancel and reset a timer but have been bombarded with different ways to try and do this, which has left me quite confused.
I'm following a timer example from here and implementing for my own situation
When this code below is run, it will output "A" every time the mouse stops. This is incorrect, as if I stop the mouse, move it quickly then stop it again, 2 sets of "A" are produced.
This carries on for however many times the stop is produced.
I believe I am missing a 'reset timer' function that will called when the mouse changes to a moving state.
How can I implement this?/Is that even the problem?
public class SomeClass{
//...some fancy code...
if (! isNowMoving) {
System.out.println("Mouse stopped!");
//Start Timer
new PrintingA(5);
} else if (isNowMoving){
System.out.println("MouseMoving");
//cancel timer & reset ready to start
}
public class PrintingA {
Timer timer;
public PrintingA(int seconds) {
timer = new Timer();
timer.schedule(new PrintingTask(), seconds * 1000);
}
class PrintingTask extends TimerTask{
#Override
public void run() {
System.out.println("A");
timer.cancel();
}
}
}
}
I'm not sure this can be useful for your requirement, Timer is 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. Read java document : java.util.Timer
I perfer to have a thread for IdleMonitor and use Apache Stopwatch to monitor idle time.
import org.apache.commons.lang3.time.StopWatch;
public class IdleMonitor implements Runnable {
StopWatch stopWatch;
private final Object monitorObj = new Object();
private boolean isActive;
private long waitTime = 6000; //in milliseconds, put appropriate time to wait
public IdleMonitor() {
isActive = true;
stopWatch = new StopWatch();
}
public void reset() { // call this during MouseMoving event
synchronized (monitorObj) {
stopWatch.reset();
monitorObj.notify();
}
}
public void finish() { // finish idle mointor operation once your operation ends, this will stop the thread
isActive = false;
reset();
}
public void start() { // start monitoring
Thread t = new Thread(IdleMonitor.this);
t.start();
}
#Override
public void run() {
synchronized (monitorObj) {
stopWatch.start();
while (isActive) {
try {
monitorObj.wait(waitTime);
} catch (InterruptedException ex) {
}
long idleTime = stopWatch.getTime();
System.out.println("Idle time " + idleTime);
// do something if idle time beyond your expected idle time.
// you could set isActive=false; if you want to stop monitoring
}
}
}
}
}

Set time between two if loops in java

Hi i want to set the time interval for few seconds between two if conditions. In my coding first "if" condition executes, control will stop 3 seconds after that only second "if" condition will execute.I dont want to use thread thread concept. I want some other option in java like "Timer" class etc.I tried many times but cant find solution . Please give solution for that
Thanks in advance
Here my code:
package com.example;
public class TimeBetween {
public static void main(String[] args) {
int a=5;
int b=3;
int c;
if(a!=0&&b!=0) {
c=a+b;
System.out.println("Addition:"+c);
}
if(a!=0&&b!=0) {
c=a-b;
System.out.println("Subtraction:"+c);
}
}
}
1) if is not loop, it is like conditional block
2) You may use sleep(milliseconds); after first if() { ...} block.
From oracle documentation:
Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing, as shown in the example that follows, and waiting for another thread with duties that are understood to have time requirements, as with the SimpleThreads example in a later section.
The most elegant solutions are either Thread.sleep or you could simulate sleep with:
Object o = new Object();
synchronized(o) {
o.wait(3000);
}
If you want to use timer you could do something like this:
But beware that the timer starts a thread in the background.
import java.util.Timer;
import java.util.TimerTask;
public class TimeBetween {
/**
* #param args
*/
public static void main(String[] args) {
final int a = 5;
final int b = 3;
Timer t = new Timer(true);
t.schedule(new TimerTask() {
#Override
public void run() {
int c;
if (a != 0 && b != 0) {
c = a + b;
System.out.println("Addition:" + c);
}
}
}, 0);
t.schedule(new TimerTask() {
#Override
public void run() {
int c;
if (a != 0 && b != 0) {
c = a - b;
System.out.println("Subtraction:" + c);
}
}
}, 3000);
}
}

Why does this java timer sample never end

I was looking for a Java timer sample and found the code below at
http://www.javaprogrammingforums.com/java-se-api-tutorials/883-how-use-timer-java.html
But if you run the sample, although it does print Timer stops now... it does not return to the command prompt. This is at least what is happening on my Windows XP machine using cmd.exe.
Why does it not return control to the prompt in this case?
import java.util.Timer;
import java.util.TimerTask;
public class TimerSample {
public static void main(String[] args) {
//1- Taking an instance of Timer class.
Timer timer = new Timer("Printer");
//2- Taking an instance of class contains your repeated method.
MyTask t = new MyTask();
//TimerTask is a class implements Runnable interface so
//You have to override run method with your certain code black
//Second Parameter is the specified the Starting Time for your timer in
//MilliSeconds or Date
//Third Parameter is the specified the Period between consecutive
//calling for the method.
timer.schedule(t, 0, 2000);
}
}
class MyTask extends TimerTask {
//times member represent calling times.
private int times = 0;
public void run() {
times++;
if (times <= 5) {
System.out.println("I'm alive...");
} else {
System.out.println("Timer stops now...");
//Stop Timer.
this.cancel();
}
}
}
It does not return to your command prompt because it is not expected to do so.
Timer creates single non-deamon thread to run all tasks. It does not terminate the thread unless you ask it. When you execture task.cancel() method you just cancel the current task, not the whole timer which is still alive and is ready to do something else.
To terminate timer you should call its stop() method, i.e. timer.stop();
In a real program you would keep a copy of the timer object and when eg program is to be closed down do a timer.cancel().
For this simple example, I added the code below after timer.schedule(t, 0, 2000);
try {
Thread.sleep(20000);
} catch(InterruptedException ex) {
System.out.println("caught " + ex.getMessage());
}
timer.cancel();
}
You need to explicitly terminate the Timer using timer.cancel(), e.g.:
class MyTask extends TimerTask {
private int times = 0;
private Timer timer;
public MyTask(Timer timer) {
this.timer = timer;
}
public void run() {
times++;
if (times <= 5) {
System.out.println("I'm alive...");
} else {
System.out.println("Timer stops now...");
//Stop Timer.
this.cancel();
this.timer.cancel();
}
}
}

I Need To Shutdown A ScheduledExecutorService, But Need To Start It Up When Needed

I made a sweet system update feature for this game I'm making here is the code:
public static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private static CountDownThread countDownThread;
public static boolean running = false;
private static short updateSeconds;
public static void start() {
System.out.println("starting");
running = true;
countDownThread = new CountDownThread();
scheduler.scheduleWithFixedDelay(countDownThread, 0, 1000, TimeUnit.MILLISECONDS);
}
public static void stop() {
System.out.println("Stoping");
scheduler.shutdown();
running = false;
updateSeconds = 0;
System.out.println("Stopped");
}
public static void refresh() {
for (Player p : Static.world.players){
if (p.ready()) {
if (updateSeconds > 0) {
ActionSender.sendSystemUpdate(p, updateSeconds+1);
} else {
ActionSender.sendSystemUpdate(p, updateSeconds);
}
}
}
}
public static short getUpdateSeconds() {
return updateSeconds;
}
public static void setUpdateSeconds(short updateSeconds) {
SystemUpdateHandler.updateSeconds = (short) (updateSeconds);
}
public static class CountDownThread implements Runnable {
#Override
public void run() {
System.out.println(updateSeconds);
updateSeconds--;
if (updateSeconds <= 0) {
Static.server.restart();
scheduler.shutdown();
running = false;
}
}
}
}
That's so, when the system update counter reaches 0, the server will restart its self. It works fine but here where the problem begins
case "update":
if (Short.parseShort(txtSystemUpdate.getText()) != 0) {
SystemUpdateHandler.setUpdateSeconds(Short.parseShort(txtSystemUpdate.getText()));
SystemUpdateHandler.refresh();
if (!SystemUpdateHandler.running) {
SystemUpdateHandler.start();
}
} else {
SystemUpdateHandler.stop();
for (Player p : Static.world.players){
if (p.ready()) {
ActionSender.sendSystemUpdate(p, 0);
}
}
}
break;
That is where I call it, basically if I enter a number higher than 0, the program works fine. But I want it so if I enter the number 0, the scheduler will stop running(to save memory) because it not needed unless I send a system update. Basically how do I stop the scheduler from running when I enter 0, but able to starts it back up when I enter a number > then 0(several times).
Once shutdown the ExecutorService can't be started again so move the creation of it from the variable declaration (and remove final) and do that in the start method instead:
//not static and not final, normal instance variable instead:
public ScheduledExecutorService scheduler;
...
//and create it in the start method isntead:
public static void start() {
System.out.println("starting");
scheduler = Executors.newSingleThreadScheduledExecutor();
...
When shutting down, you will get list of tasks submitted to the scheduler, and you can use this list to create new one. Scheduler can not be started once stopped - because thread pool is dead and all worker threads are dead as well.

Running code when all threads are finished processing

Quick note: Java and Android noob here, I'm open to you telling me I'm stupid (as long as you tell me why.)
I have an android application which requires me start multiple threads originating from various classes and only advance to the next activity once all threads have done their job. I also want to add a "failsafe" timeout in case one the the threads takes too long (HTTP request taking too long or something.)
I searched Stack Overflow and found a post saying that I should create a class to keep a running total of open threads and then use a timer to poll for when all the threads are completed.
I think I've created a working class to do this for me, it's untested as of yet but has no errors showing in eclipse.
Is this a correct implementation? Are there any APIs that I should be made aware of (such as classes in the Java or Android APIs that could be used in place of the abstract classes at the bottom of the class?)
package com.dmp.geofix.libs;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
public class ThreadMonitor {
private Timer timer = null;
private TimerTask timerTask = null;
private OnSuccess onSuccess = null;
private OnError onError = null;
private static ArrayList<Thread> threads;
private final int POLL_OPEN_THREADS = 100;
private final int TIMEOUT = 10000;
public ThreadMonitor() {
timerTask = new PollThreadsTask();
}
public ThreadMonitor(OnSuccess s) {
timerTask = new PollThreadsTask();
onSuccess = s;
}
public ThreadMonitor(OnError e) {
timerTask = new PollThreadsTask();
onError = e;
}
public ThreadMonitor(OnSuccess s, OnError e) {
timerTask = new PollThreadsTask();
onSuccess = s;
onError = e;
}
public void start() {
Iterator<Thread> i = threads.iterator();
while (i.hasNext()) {
i.next().start();
}
timer = new Timer();
timer.schedule(timerTask, 0, POLL_OPEN_THREADS);
}
public void finish() {
Iterator<Thread> i = threads.iterator();
while (i.hasNext()) {
i.next().interrupt();
}
threads.clear();
timer.cancel();
}
public void addThread(Thread t) {
threads.add(t);
}
public void removeThread(Thread t) {
threads.remove(t);
t.interrupt();
}
class PollThreadsTask extends TimerTask {
private int timeElapsed = 0;
#Override
public void run() {
timeElapsed += POLL_OPEN_THREADS;
if (timeElapsed <= TIMEOUT) {
if (threads.isEmpty() == false) {
if (onSuccess != null) {
onSuccess.run();
}
}
} else {
if (onError != null) {
onError.run();
}
finish();
}
}
}
public abstract class OnSuccess {
public abstract void run();
}
public abstract class OnError {
public abstract void run();
}
}
Take a look at Thread.join
Check java.util.concurrent package .
specifically Future, FutureTask and ExecutorService
FutureTask allows to get the status of the operation under execution with methid isDone()
I hope this helps
All the threads should be implementing callable and they should be future tasks.
Start all of them with countdownlatch/CyclicBarrier to ensure all of them start at same time
If you intend for the application to exit after running the code, you might want to have a look at Runtime.getRuntime().addShutdownHook(Thread) to specify a thread to be executed when the application is exiting.

Categories