Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  improving java's Timer and TimerTask  (Read 3520 times)
0 Members and 1 Guest are viewing this topic.
Offline emzic

Senior Member





« Posted 2007-12-19 12:56:25 »

hello,

java has a nice bundle of classes to schedule execution. namely java.util.Timer and java.util.TimerTask. unfortunately they work with currentTimeMillis() which is just not accurate enough for realtime games.

so i was trying to replace currentTimeMillis() with nanoTime(), but now my schedulded tasks are executed once and then never again.
i was already looking through relevant code if there are any precision errors like a division or a conversion from milliseconds to seconds which would need adaption of course. but i couldnt find anything.

does anyone know why java.util.Timer does not work anymore when replacing millis with nanos?

thanks!

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline Mr_Light

Senior Member




shiny.


« Reply #1 - Posted 2007-12-19 19:31:52 »

depending on what you need it for: https://timingframework.dev.java.net/

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #2 - Posted 2007-12-19 20:47:28 »

System.currentTimeMillis() is in milli-seconds
System.nanoTime() is in nano-seconds.

So simply replacing one with the other - like you stated - is almost correct.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline emzic

Senior Member





« Reply #3 - Posted 2007-12-20 15:21:48 »

thanks, yeah, that's why i was looking for code which could be specific to nanos or millis, which could be the cause for the repeating execution not working.

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline emzic

Senior Member





« Reply #4 - Posted 2007-12-28 13:53:53 »

i'm still stuck with this problem... does anyone have an idea what i could be doing wrong? thanks!

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline DzzD
« Reply #5 - Posted 2007-12-28 15:24:36 »

maybe try to use a "proxy" class as timer that call System.currentTimeMillis to keep some trace of what happen than do the same with nanosecond timer. it may give you some informations on what cause this issue ?

Offline emzic

Senior Member





« Reply #6 - Posted 2007-12-29 22:52:22 »

thanks for the info.

i am now doing a testcase to debug it further. seems there is an Object.wait() somewhere which takes milliseconds as arguments.

still it is not working very well.... i gotta look into this some more deeper.

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #7 - Posted 2007-12-29 23:19:20 »

1  
2  
3  
4  
public static long milliTime()
{
   return System.nanoTime() / 1000000L;
}


?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (34 views)
2013-05-17 21:29:12

alaslipknot (42 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (71 views)
2013-05-16 00:17:58

theagentd (79 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (116 views)
2013-05-14 15:56:26

kutucuk (139 views)
2013-05-12 17:10:36

kutucuk (140 views)
2013-05-12 15:36:09

UnluckyDevil (147 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.115 seconds with 20 queries.