Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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  
  Thread.yield()  (Read 1048 times)
0 Members and 1 Guest are viewing this topic.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Posted 2012-12-18 01:48:26 »

I was just looking at my 4k game and I noticed Thread.yield() - copied from the applet templates thread (http://www.java-gaming.org/topics/applet-templates/21626/view.html), specifically groboclown's reply number 5 (it's from 2009)

I was wondering what it did so I read this (http://www.javamex.com/tutorials/threads/yield.shtml) and it says not to use thread.yield, instead to use thread.sleep, however I seem to hear people recommending against thread.sleep(). Exactly what is so bad about yield, and what should I use instead?
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-12-18 02:31:44 »

Thread.yield() is basically just a suggestion, and the platform is free to ignore that suggestion.  I remember reading some code for Hotspot on Solaris where it does exactly that if you yield too many times in a short interval.  Even if it always yields, it still forces immediate re-scheduling, which can interfere with the CPU actually sleeping when idle.

If you really have nothing to do, sleeping even a millisecond in a tight loop can save you a lot of CPU usage over a bare yield.  Mobile users will thank you.  Now if you have precise frame deadlines and your timer is out of whack (that is, you're on Windows), then Thread.yield() in a spin loop might be exactly what you need.



Offline LunaticEdit

Senior Member


Medals: 8
Projects: 1



« Reply #2 - Posted 2012-12-26 23:34:47 »

I tried several methods, and yield makes no sense in a game loop. It tells the scheduler that works it's doing is not that important and that it can go ahead and move to the next time slice. What I do in my tight render loop is lock it at a max of 59.9something FPS:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
    public void run() {
        while (_running) {
            final long newTime = System.currentTimeMillis();

            if ((newTime - _lastProcessed) < GAME_MINFRAMETIME) {
                try {
                    Thread.sleep((long) (GAME_MINFRAMETIME - (newTime - _lastProcessed)));
                } catch (final InterruptedException e) {
                    e.printStackTrace();
                }
                continue;
            }
            _lastProcessed = newTime;
            try {
                renderGraphics();
                handleMessages();
                if (_currentScreen != null) {
                    _currentScreen.update();
                }
            } catch (final Exception e) {
                e.printStackTrace();
                stop();
            }
        }
    }


This slows the thread down if it's running 60+ fps. When I look in the profiler it shows the thread obediently sleeping when needed, allowing other threaded loops to execute before returning back to the render logic.

Thread.Sleep tells the system to not execute the thread for x amount of time -- this means it literally gives absolutely 0 CPU time to the thread until the time has passed (this is an operating system related thing). I highly prefer thread.sleep when used properly.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 43



« Reply #3 - Posted 2012-12-26 23:58:14 »

I tried several methods, and yield makes no sense in a game loop. (...)
Thats a sound statement. You clearly have not tested all OS/CPU combinations out there. There are a lot of threads in this forum regarding game loops and there are enough posts stating, that a tight yield loop is the only method getting a stable frame-rate on their OS/CPU combination.

Still it remains true, that a Thread.sleep() should be prefered and there is also a deamon-thread trick, that seems to help on at least some windows systems to get a reliable sleep game loop...

Mathias - I Know What [you] Did Last Summer!
Offline LunaticEdit

Senior Member


Medals: 8
Projects: 1



« Reply #4 - Posted 2012-12-27 00:03:24 »

Thats a sound statement. You clearly have not tested all OS/CPU combinations out there. There are a lot of threads in this forum regarding game loops and there are enough posts stating, that a tight yield loop is the only method getting a stable frame-rate on their OS/CPU combination.

Are you suggesting that it is possible to get a 'stable' frame rate in ANY system? If so I have never seen it -- indy games or professional. Framerate will always jump around. You literally cannot run at exactly 60 fps. If frame rate makes your game 'laggy' then you are frame-locking your rendering logic, which is WRONG to begin with, as any FPS under the 'intended' will slow the game down, vs making it choppy. All movement and animation should be done based on time passed, not frames passed.

Offline cylab

JGO Ninja


Medals: 43



« Reply #5 - Posted 2012-12-27 00:16:23 »

Nope. I don't think it will be possible to get a stable timestep (lets not call it framerate anymore) on ANY system, just on some more systems. I didn't want to emphasis the use of Thread.yield(), I just wanted to point out, that you were exeggarating in your statement.

Btw. a tight loop with Thread.sleep(1) along with the deamon-trick seems to be the right tradeoff between CPU usage and accuracy.

Regarding the frame-locking - there is also a lot of threads about fixed vs. variable timestep game loops and the consensus seems to be: use the one you think fits best in your targetted environment - if framelocking appears either switch the method or optimize performance Wink

Mathias - I Know What [you] Did Last Summer!
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.

atombrot (27 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!