thread problem in suspend() and resume()
hi all i doing a stop watch. for pause i use Thread.suspend() and resume i use Thread.resume(). but the resume is not resume the work. code:
pause(){
shouldRun = fa开发者_如何学运维lse;
currentThread.suspend();
}
resume(){
shouldRun = true;
currentThread.resume();
}
while(shouldRun){ ....... }
There's a reason why Thread.suspend()
and Thread.resume()
are deprecated - they're not a good idea for various reasons. Most importantly, the thread itself is in the best position to know how to pause safely (e.g. while not holding a lock).
I urge you to reconsider your design to avoid using suspend/resume. If you tell us more about what you're trying to achieve, we may be able to help you more.
If you really want to suspend a Thread for some time have al look at java synchroniziation and especially Object.wait() and Object.notify/notifyAll()
I dont't really get why you are using Thread.suspend() and resume for a stopwatch application.
Why don't you just get System.currentTimeMillis() each time the user presses the stopwatch button and compute the time delta?
Google says an application should react in 100 or 200 ms : http://developer.android.com/guide/practices/design/responsiveness.html
You should use Thread.Sleep() function in your thread like that: public void run(){ do{ Thread.Sleep(200); if(shouldRun){ doSomething(); //do update your clockwatch } while(contidions); }
Beware not doing any UI updates directly in a worker thread : Use and Handler to send your UI updates to the main thread : just UI updates functions.
精彩评论