Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  Java System System.currentTimeMillis(); error  (Read 2039 times)
0 Members and 1 Guest are viewing this topic.
Offline DQQAYME

Junior Member


Medals: 1
Projects: 1



« Posted 2013-06-04 22:42:59 »

Today using LWJGL(not sure maybe problem only with him)
I find error
1  
2  
3  
Thread.sleep(10); // sometimes after call sleep
System.currentTimeMillis();// milis returns same as before sleep
//Nano sec return right value


 I use this code to synchronize physics tic
1  
2  
3  
4  
      long time = System.currentTimeMillis();
      Frames_Dif_Milis = time - Frame_End_Milis;
      Frame_End_Milis = time;
      Thread.sleep(10);

But some times Frames_Dif_Milis == 0; Java 1.7
To fix error I use now nano sec difference Smiley

Offline Kerai

Junior Member


Medals: 4



« Reply #1 - Posted 2013-06-05 00:42:12 »

That's probably a Windos bug - as it is the only OS that doesnt return accurate milisecond timer. It rounds it to 16 miliseconds.

Your best bet is to use System.nanoTime(); which doesnt use that retarder windows pipeline underneath.

1  
2  
3  
4  
5  
long time = System.nanoTime();

// do stuff

time = (System.nanoTime() - time) / 1000000; // here you go, accurate miliseconds
Offline DQQAYME

Junior Member


Medals: 1
Projects: 1



« Reply #2 - Posted 2013-06-05 01:10:15 »

Ty i fix for nanosec Wink

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online SHC
« Reply #3 - Posted 2013-06-05 03:35:21 »

A more easier solution is to create a method
getCurrentTime()
to convert the nanos into millis and return them. Here's it.

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

Then to synchronize physics, your snippet becomes

1  
2  
3  
4  
long time = getCurrentTime();
Frames_Dif_Milis = time - Frame_End_Milis;
Frame_End_Milis = time;
Thread.sleep(10);

Don't forget to replace all calls to
System.currentTimeMillis()
to
MyGameUtil.getCurrentTime()

Offline ags1

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« Reply #4 - Posted 2013-06-05 06:50:12 »

That's probably a Windos bug - as it is the only OS that doesnt return accurate milisecond timer. It rounds it to 16 miliseconds.

Your best bet is to use System.nanoTime(); which doesnt use that retarder windows pipeline underneath.

1  
2  
3  
4  
5  
long time = System.nanoTime();

// do stuff

time = (System.nanoTime() - time) / 1000000; // here you go, accurate miliseconds


I've read about the 16 ms accuracy on windows before, and I recently tested this in Win 7 64-bit. At least on that OS, I could not reproduce the claimed 16ms rounding. Even for a short sleep like 5ms, the currentTimeMillis() result only differed from nanoTime() by 1ms:

http://www.java-gaming.org/topics/system-nanotime-versus-system-currenttimemillis/29759/view.html

Offline gimbal

JGO Knight


Medals: 25



« Reply #5 - Posted 2013-06-05 16:02:45 »

It might be that on Windows Vista/7 it is actually not relevant anymore, I never tested that. Its not really a "bug", its just a piece of retarded legacy in the Win32 API that I believe goes all the way back to Windows 3.x. You can set the system-wide precision yourself with an API call, many applications which require high precision timing (video playback software, games) actually do that. On WindowsXP it is certainly still relevant, on some systems the precision may be 10ms and on others 15ms to make it even more difficult. So if you want your stuff to work properly on WindowsXP, better not ignore it.

You can test it out if you still have XP: activate a movie player (VLC or something) and while the movie is playing, run your Java application and check the sleep timing precision. You'll find it is actually 1 or 2ms as if by magic, if the other software actually changes the precision of course.
Offline gouessej
« Reply #6 - Posted 2013-06-05 19:48:53 »

Hi

You should look at this bug report:
http://bugs.sun.com/view_bug.do?bug_id=6435126

We already mentioned it here:
http://www.java-gaming.org/topics/solved-ghost-thread-eating-performance-lwjgl/24792/msg/210847/view.html#msg210847

Offline gimbal

JGO Knight


Medals: 25



« Reply #7 - Posted 2013-06-06 15:08:47 »

indeed and in LWJGL (and by extension LibGDX) the workaround fix is actually implemented, which is a very nice thing indeed Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

pw (15 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!