我说不出这股力量是什么,我只知道它存在。     --贝尔
首页 arrow 开发技术 arrow 新手上路 arrow Do not use Thread.yield()

Do not use Thread.yield() 输出PDF 打印 E-mail
Author Author: 一滴蔚蓝色 | Date Date: 2008-07-03 | View Count View: 438 | section & Category 开发技术 -  新手上路

转自: http://lyo.blogsome.com/

I refactor the EventQueue in KEmulator, then test on some games.

when running MI3 and Gangstar, the CPU reached 100% utilization.

and after testing, i got my answer, cause of Thread.yield() in run() with a while(true) circle.

 

So i made a test, preReplace all the Thread.yield() with Thread.sleep(1) in game loading. this time, everything goes good. the MI3 got a lower 10% CPU utilization in average.

  • sleep() will let your thread do nothing for a certain amount of time. Basically, your thread can’t do anything until it is done sleeping.
  • yield() will cause your thread to voluntarily let other threads in your program run. If no other threads are ready to run, then your thread will continue.

Thread.yield() will help your thread give time to other threads, but when other threads are free or yield()ing will still take up 100% of the CPU. Otherwise, Thread.sleep() is the only way to force the CPU getting a rest.

 

=======================

May be there is a same case in Mobile development.

In some handset, when the game reaches the memory limit, the key events will cause an obviously delay. In Realfootball team last year, i found out that calling more Thread.yield()(two or three) in the run() circle can solve the problem.

Maybe one Thread.sleep(1) did the job here.

 



收藏到您的网摘: Google书签 Yahoo书签 雅虎收藏夹 365Key网摘 新浪ViVi 百度收藏 天极网摘 diglog 和讯网摘 POCO网摘 YouNote网摘 博拉网 天下图摘 spurl blogmarks BlinkList reddit digg Del.icio.us

本文关键字本文关键字: use  Thread  yield  

阅读数: 439 | 打印 | E-mail

  我要评论
RSS评论

发表评论

姓名:
E-mail
您的网站/主页
标题:
评论:

验证码:* Code
若有人评论本文,请E-mail通知我。

(J2ME FAQ) < 上一篇   下一篇 > (关于手机Java游戏模拟器)