程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> Do not use Thread.yield()

Do not use Thread.yield()

編輯:J2ME

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.

 

Soi made a test, preReplace all the Thread.yIEld() with Thread.sleep(1)in game loading. this time, everything goes good. the MI3 got a lower10% CPU utilization in average.

sleep() will let your thread do nothing for a certain amount oftime. Basically, your thread can’t do anything until it is donesleeping.yIEld() will cause your thread to voluntarily letother threads in your program run. If no other threads are ready torun, then your thread will continue.

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

 

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

May be there is a same case in Mobile development.

Insome handset, when the game reaches the memory limit, the key eventswill cause an obviously delay. In Realfootball team last year, i foundout that calling more Thread.yIEld()(two or three) in the run() circlecan solve the problem.

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved