Run a thread every two seconds

How can I run a thread every two seconds without using the Timer object?

[79 byte] By [java_swing_dudea] at [2007-11-27 11:44:12]
# 1

Something wrong with using timers?

BigDaddyLoveHandlesa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 2

The Timer generates a new thread which just seems to keep increasing the thread count of my app

java_swing_dudea at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 3

What implementation of timer are you using?

BigDaddyLoveHandlesa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 4

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);

java_swing_dudea at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 5

To answer my own question: java.util.Timer.

BigDaddyLoveHandlesa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 6

Don't create a new thread pool every time run is called?

BigDaddyLoveHandlesa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 7

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...

java_swing_dudea at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 8

swing programmers should only post on the swing forum.

abillconsla at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 9

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

BigDaddyLoveHandlesa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 10

You want to run a new and different Thread each 2 seconds?

abillconsla at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 11

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.

DrClapa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 12

So how can I gain accurate timings for the thread?

java_swing_dudea at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 13

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)

tswaina at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 14

Er, did you read the replies? He thought Timer was misbehaving, but it was really his run method at fault.

BigDaddyLoveHandlesa at 2007-7-29 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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)

tswaina at 2007-7-29 17:54:21 > top of Java-index,Java Essentials,Java Programming...