Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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  
  Is a busy-wait Thead.yield() a good idea?  (Read 5047 times)
0 Members and 1 Guest are viewing this topic.
Offline ShannonSmith
« Posted 2012-03-28 01:31:38 »

If you want to run at a fixed 60FPS and don't have access to VSync doing a constant Thread.yield() checking System.nanoTime() appears to be the only way to get decent timing. Thread.sleep() is usually +/- 10ms which is unacceptable for trying to get a consistent 16.7ms per frame.
I'm a little worried that Thread.yield() will be too CPU intensive and have platform dependent issues and was wondering if anyone has any insight into how Thread.yield() works or a better way to avoid high CPU usage but also achieve reliably timing.
I've tried using Java Audio but the SourceDataLine blocking behavior is just as bad as Thread.sleep().
Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-03-28 01:34:48 »

I'm going to shamelessly plug my own implementation, that is now in the nightly build of LWJGL:

http://java-game-lib.svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/opengl/Sync.java?revision=3758&view=markup

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ShannonSmith
« Reply #2 - Posted 2012-03-28 01:48:58 »

Looks interesting, how well does it work? What is the typical number of sleep(1) calls per frame? How often does it over-sleep and by how much?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-03-28 01:55:28 »

Every few minutes it misses its scheduled frame time by slightly over 1ms.

In other words: it's extremely accurate.

The number of sleeps is irrelevant, you can easily calculate it. It's all about reducing the number of yields. On my CPU there are about 50-100 yields per frame, which I think is quite low.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ShannonSmith
« Reply #4 - Posted 2012-03-28 15:52:17 »

This article is quite interesting:

http://www.javamex.com/tutorials/threads/yield.shtml

And it's probably worth testing your sync code on a few platforms to see if the behavior is consistent.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #5 - Posted 2012-03-28 16:37:31 »

And it's probably worth testing your sync code on a few platforms to see if the behavior is consistent.
I've done quite a bit of cross platform testing on that method and IMO its probably as accurate as its going to get with those methods (while also not totally killing the CPU) but more testing is always good Smiley
Offline Damocles
« Reply #6 - Posted 2012-03-28 17:00:19 »

How about I run a Java2D game with this at 40FPS on a normal flatscreen, will this prevent tearing then?
Or should it run at 60?

Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-03-28 17:05:22 »

As long as your FPS is less than the refresh rate, you don't get tearing.  If it's not an integer divisor of your framerate, you may get noticeable stutter however.  Isn't Java2D supposed to respect your desktop's compositor anyway?  That means on windows it should never tear, and on linux it always will.
Offline Cero
« Reply #8 - Posted 2012-03-28 17:53:39 »

So, in the new nightly build Display.sync() does exactly this ? Or should one copy that class and use it instead ?

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2012-03-28 18:10:31 »

So, in the new nightly build Display.sync() does exactly this ? Or should one copy that class and use it instead ?
Display.sync(fps) calls Sync.sync(fps)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2012-03-28 18:17:29 »

So, in the new nightly build Display.sync() does exactly this ? Or should one copy that class and use it instead ?
Display.sync(fps) calls Sync.sync(fps)

k, and initialise() is called internally by lwjgl, I presume.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2012-03-28 18:23:44 »

Yes, if only because it's a private method.

If you want to have a jolly good time, try these:

 Wink

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #12 - Posted 2012-03-28 22:39:48 »

As long as your FPS is less than the refresh rate, you don't get tearing.  If it's not an integer divisor of your framerate, you may get noticeable stutter however.  Isn't Java2D supposed to respect your desktop's compositor anyway?  That means on windows it should never tear, and on linux it always will.
False. You can get tearing at any FPS, but it's most noticeable at higher frame rates. Picture a road intersection. You just stated that we don't need traffic lights or stop signs as long as the road isn't completely full of cars.

Myomyomyo.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #13 - Posted 2012-03-28 22:57:55 »

Right, I was way off by saying you never get tearing, you still need to enable vsync to avoid it entirely.  It's just less apparent on many LCD monitors largely because the refresh itself usually completes long before the mandatory vertical blank interval comes around.

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.

Grunnt (13 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!