Timers/Thread preformance?
6 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mikael_Rickan
Posted On:   Tuesday, November 20, 2001 08:45 PM

Hi there.


I need some kind o high preformace timing in my prog, where one method should be called every 40ms, nothing more, nothing less.. (or atlease as close as possible)..


The method called consumes very much time. (near the availible 40ms, sometime more).


If the time has passedby i reconfig the timer to get 40-extratime.


Should I use some kind of Thread or Timers? any ideas?

Re: Timers/Thread preformance?

Posted By:   Anonymous  
Posted On:   Sunday, August 29, 2010 09:35 AM

near the availible 40ms, sometime more

Re: Timers/Thread preformance?

Posted By:   Anonymous  
Posted On:   Saturday, April 28, 2007 04:24 AM


Hi Mikael Rickan,
With java.util.Timer, java.util.TimerTask classes tasks may be scheduled for one time execution or for repeated executions. Timer class is having overloaded schedule() methods,

1)to execute specific tasks at the specified time
2)or for repeated fixed delay execution beginning at the specified time
3)or after the specified delay
4)or for repeated fixed delay execution beginning after the specified delay
5)or for repeated fixed rate execution beginning at the specified time
6)or for repeated fixed rate execution beginning after the specified delay

Timer class uses TimerTask thread and the method calling will be written in the TimerTask's run().

A Sample program,

public class TimerExample {
Timer timer;


public TimerExample ( long seconds ) {
timer = new Timer ( ) ;
timer.schedule ( new TimerExampleTask ( ) , seconds*1000 ) ;
}


class TimerExampleTask extends TimerTask {
public void run ( ) {
TimerExample.timerMethod1();
}
}

public static void timerMethod1()
{
//Logic
}


public static void main ( String args [ ] ) {
System.out.println ( "Task Schedule Started" ) ;
new TimerExample ( 40 ) ;
System.out.println ( "Task scheduled." ) ;
}
}

In the above class TimerExapmpleTask scheduler will call TimerExample's timerMethod1() for every 40ms(this comes under 3rd category in the above list, i.e. task execution after the specified delay).

Thanks & Regards
Soma Raja S.

Re: Timers/Thread preformance?

Posted By:   hari_haran  
Posted On:   Friday, May 14, 2004 07:12 AM

hay,


I have a small logic for you....just try implementing this, what you do is create a SCHEDULER THREAD which extends a normal Thread class, now in run method keep a while loop and make it sleep for 35 milli seconds, now keep a static hash map or a vector according to your convinence, and store the thread in that, which needs to be invoked every 40 milliseconds(Let us call it it Thread "A" ). For every 35 milliseconds try to check whether the Thread "A" is running or not If its running just dont do any task, if its not running call start on it.



now, how to know whether a thread is running or not explictly?

the answer would be keep a boolean flag in Thread "A", and change it to true in the start of run() method and false at the end of run() method.



If u require more info harihkrishna@yahoo.com

Re: Timers/Thread preformance?

Posted By:   Chandra_Goliva  
Posted On:   Monday, August 18, 2003 04:49 PM


Using a thread would address your problem. I am giving the high level program flow for your quick ref-

(This ensures your process to execute at every 40 milli seconds and/or adjusts to the next 40 th millisec if it takes longer than that..Does it not sound great!!)

Define a class which implements a Runnable interface, which means to define a run() method. And for your problem your method call will be placed in it.(say DaemonProcedure.class)

a snippet inside run() method:
run(){

long PERIODICITY=40;//as you wished!

long t1, t2, tTask;
...
...
while(true){
t1=System.currentTimeMilliSec();
...
/**PERFORM THE TASK THAT YOU WANNA DO*/
performTask();

...
t2=System.currentTimeMilliSec();
tTask=t2-t1;

if(tTask <= PERIODICITY){

//when task takes less than preset time
Thread.sleep(PERIODICITY-tTask);

}else{

/*
when task takes longer than preset time then adjust the
next task execution
*/
Thread.sleep(PERIODICITY - (tTask%PERIODICITY));

}
}//end of while()

}//end of run()


How to set up?(Better be done in some application level/server level start up class/procedure):

1.Instantiate a new object of the newly defined Runnable class.(say runnableObj)
DaemonProcedure runnableObj=new DaemonProcedure();

2.Instantiate a
Thread t=new Thread(runnableObj)
with the previosly initialized runnableObj as an argument.

3. Start execution of the Thread.
t.start();

Re: Timers/Thread preformance?

Posted By:   Rajeev_Mahajan  
Posted On:   Thursday, January 9, 2003 08:10 AM

In a normal setup this is not possible. You can get some more close result by turning of the GC (be careful if you turn it off). or else you will have to use real time java. Also search for some real time apis in java. java.util.Timer class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method

Rajeev

Re: Timers/Thread preformance?

Posted By:   Shashi_Bhusan  
Posted On:   Friday, November 30, 2001 01:02 AM

As per your requirements I feel you should use java threads. I have used threads on many such problems, ofcourse not at 40 ms. As documented in Java API "If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.". This is just opposite to your requirements.

Moreover This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method. Thread.sleep() method is quite reliable for your usage. Hope this helps you
About | Sitemap | Contact