Run a thread every two seconds
How can I run a thread every two seconds without using the Timer object?
How can I run a thread every two seconds without using the Timer object?
final Timer timer = new Timer();
int seconds = 2;
timer.schedule(new TimerTask() {
@Override
public void run() {
Executor exec = Executors.newCachedThreadPool();
exec.execute(new CalculatorEngine(dirtyPositionsSet1, "Set1", SYNC_CALC_OBJ1));
exec.execute(new CalculatorEngine(dirtyPositionsSet2, "Set2", SYNC_CALC_OBJ2));
exec.execute(new CalculatorEngine(dirtyPositionsSet3, "Set3", SYNC_CALC_OBJ3));
exec.execute(new CalculatorEngine(dirtyPositionsSet4, "Set4", SYNC_CALC_OBJ4));
exec.execute(new CalculatorEngine(dirtyPositionsSet5, "Set5", SYNC_CALC_OBJ5));
exec.execute(new CalculatorEngine(dirtyPositionsSet6, "Set6", SYNC_CALC_OBJ6));
exec.execute(new CalculatorEngine(dirtyPositionsSet7, "Set7", SYNC_CALC_OBJ7));
}, seconds * 1000, seconds * 1000);
D'oh! Silly mistake, thank you for pointing it out!!
By the way you may know the answer to this as well?!?
How do I accurately gain timings for a thread? These threads are calling CalculatorEngine which locks on an object starts a timer (ctime = System.currentTimeMillis();) and then logs the time just before the thread exits
System.out.println("Calculations for " + setName + " for " + calcsMadeByCalcEnginge + " positions took: " + (System.currentTimeMillis() - ctime));
But if there is a context swich my timer will continue to run until the thread is reactivated and then completes...
This is out of my area, but if precise timing is an issue, that may be a job
for Real Time Java.
http://java.sun.com/javase/technologies/realtime/index.jsp
Right. That code gives you the elapsed time for a thread. I don't think there's any way to get the CPU time used by a thread, if that's what you are trying to ask for.
A different implementation implementing waits....
import java.util.*;
import java.text.SimpleDateFormat;
public class Clock implements Runnable
{
boolean running;
Thread task;
Date date;
Clock()
{
date = new Date();
start();
}
public void start()
{
task = new Thread(this);
task.start();
}
public void stop()
{
running = true;
}
public void run()
{
long now;
String clock;
SimpleDateFormat dateSyntax = new SimpleDateFormat("s",Locale.getDefault());
dateSyntax.applyPattern("EEE MMM dd HH:mm:ss yyyy");
//- synch for preemptive wait()
synchronized(this)
{
running = true;
while(running)
{
now = System.currentTimeMillis();
date.setTime(now);
clock = dateSyntax.format(date);
System.out.println(clock);
//-- Add your code here...
// ........ {Some code} .........
// (long) timeTake = (long)(System.currentTimeMillis() - (long)now);
try
{
wait(1000);
}
catch(InterruptedException ie)
{
}
}
notifyAll();
}
}
public static void main(String args[])
{
try
{
new Clock().task.join();
}
catch(InterruptedException ie)
{
}
}
}
Anyway thats about as accurate as you will be able to get.
It used to be about +=50MS (not including context switch overhead)
But I haven't tested this recently so it may have improved.
Good Luck!
(T)
Er, did you read the replies? He thought Timer was misbehaving, but it was really his run method at fault.
> Er, did you read the replies? He thought Timer was
> misbehaving, but it was really his run method at
> fault.
No :)
Just showing how I would run something every 2 seconds.
(T)