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  
  Thread.yield() doesn't do what most think it does  (Read 2199 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Posted 2005-07-27 12:29:52 »

Thread.yield() will only work if there are other threads with the same priority otherwise the request is ignored.

All the well designed timers use the yield command in conjunction with a high precision clock, the problem with that is you will not be able to fix your framerate if your thread is the only one running with the same priority.
The issue hasn't occured as of yet because even if you don't use AWT/Swing there are other things running behind the scenes but what happens if in future all these things were no longer required and the only thread running is your own?

Everytime you load your game it will load it's own instance of the JVM, if you aren't using Swing/Awt then the only thread running will be your own and your timer will not work.

Other than going straight to JNI or using sleep(because it's innacurate) what other alternatives are there to Thread.yield()?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2005-07-27 14:02:14 »

There are no other alternatives. And FYI, Thread.yield() works fine under any normal gaming circumstances although I personally find Thread.sleep(0) to be slightly better.

Cas Smiley

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #2 - Posted 2005-07-27 14:39:23 »

AFAIK the minimum you can sleep for is dependent on the OS.
Windows XP = 10ms.

Thread.sleep(0) will sleep for 10ms under XP, 50ms under 98 and I'm not sure but probably wont sleep at all under Linux.

Do you use sleep(0) instead of yield in your games?

There are no other alternatives. And FYI, Thread.yield() works fine under any normal gaming circumstances although I personally find Thread.sleep(0) to be slightly better.

Cas Smiley

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2005-07-27 15:40:04 »

I do now, as I discovered that a busy yield() loop uses so much CPU (albeit "kindly") on my laptop it triggered the heat sensor and clocked down the processor Sad

A busy sleep(0) seems to work just as well for my games.

Cas Smiley

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #4 - Posted 2005-07-27 17:33:12 »

I think I'm going to have to eat my words yet again.
Thread.yield() as of Java 5 should be deprecated.

Quote
Nanosecond-granularity timing: The new java.lang.System.nanotime() method that provides high-precision timing facilities has been added to enable access to nanosecond-granularity time source for making relative time measurements, and methods that accept timeouts such as BlockingQueue.offer(), BlockingQueue.poll(), Lock.tryLock(), Condition.wait(), and Thread.sleep(). Note that the precision of nanotime() is platform-dependent.

You should take note that Thread.sleep() has NS accuracy.
I suggest you try Thread.sleep(1) for more appropriate results Princec. Wink

http://java.sun.com/developer/technicalArticles/J2SE/concurrency/

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2005-07-27 18:11:48 »

Sorry I was in fact talking out of my arse Smiley I do use sleep(1), not sleep(0).

Cas Smiley

Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #6 - Posted 2005-09-08 22:01:44 »

I'm wondering if using Thread.sleep(1) might not starve the CPU if for example the GC has significant work to do or hotspot is doing intensive optimizations/compilations? This might not be significant on recent or fast systems but maybe not on older ones.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2005-09-08 22:28:15 »

I'm wondering if using Thread.sleep(1) might not starve the CPU if for example the GC has significant work to do or hotspot is doing intensive optimizations/compilations? This might not be significant on recent or fast systems but maybe not on older ones.
On any non-realtime OS (read: any desktop OS) sleep(mills) and its varients specify the minimum amount of time to sleep for. The OS can quite happily keep your thread dormant for much longer if some other process - such as the GC - is keeping it plenty busy.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
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 (17 views)
2014-07-24 01:59:36

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

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

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

ctomni231 (45 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!