I am developing a chess game for Android (http://androidchess.appspot.com), using SurfaceView
for the chessboard. I have a drawing Thread
, that draws the chessboard in a loop. The problem is that when there are no active animations (this is more that 90% of time), it makes no sense to waste CPU and battery for drawing. How should I solve this? Maybe somehow pausing and resuming the drawing Thread
?
views:
364answers:
2
A:
You pretty much answered the question yourself. You should pause your drawing thread when nothing is rendered. When using the OpenGL SurfaceView you can also use an immediate mode (which draws continuously) or manual (draw by request).
Soulreaper
2010-03-20 15:02:25
But how exactly should I pause/resume the thread? The Thread.suspend() is deprecated according to Android's documentation.
fhucho
2010-03-20 15:09:34
the above approach from lirik would work i presume. Another more modern approach would be to use the java concurrency classes in combination with a Lock/Condition or similar approaches.
Soulreaper
2010-03-20 18:51:39
+1
A:
Try something like this (you can test it by trying this code):
final Object monitor = new Object();
class Drawing implements Runnable
{
@override
void run()
{
while(true)
{
synchronized(monitor)
{
monitor.wait();
}
// draw
}
}
}
public static void main(String[] args) {
// start your thread
while(true)
{
if( needRefresh )
{
synchronized(monitor)
{
monitor.notify();
}
}
}
}
I would recommend using a Semaphore if you want to signal once and then keep drawing (i.e. do not block) until the signal is turned off.
Lirik
2010-03-20 16:33:02