On the road to learning Android programming, there was a big disaster.
Public void run (){
While (gameloop ){
Log. d ("update", "free mem (Main) =" + Runtime. getRuntime (). freeMemory ());
If (System. currentTimeMillis ()-time & gt; 200 ){
Log. d ("update", "GameView. run ()");
Synchronized (holder ){
Canvas canvas = null;
Try {
Canvas = holder. lockcanvas ();
Canvas. drawrgb (0, 0, 0 );
Curscene. Update (canvas );
// Holder. unlockcanvasandpost (canvas );
Time = system. currenttimemillis ();
} Catch (exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
If (canvas! = Null ){
Holder. unlockCanvasAndPost (canvas );
}
}
}
}
}
}
This Code does not seem to have any problems. Update everything in the scenario in curscene. Update (), including actions and animations/
But one day, the animation in the game could not be updated. Finally, we found that the run () method was called once or twice and it was no longer called. As a result, the map and player images could not be updated.
As a result, I first began to wonder if Java had recycled my memory (the garbage collection prompt also exists in the log, and I also used a lot of static variables and Singleton, so I am a little worried about the memory ). However, I found that the coordinates of my players can be modified, that is, my mobile operations. Players are moved, but they cannot be displayed on the screen. This may also be because the memory cannot be controlled in your own hands during the long-term C ++ and Java operations, which may be caused by some uneasiness.
Then, I began to think about whether the thread was blocked. I searched the internet and couldn't find good information (in fact, the thread blocking answer is close to the truth). I didn't find any information.
Finally, my friend woke up with a single sentence and said, "Is there an endless loop "!!! So I commented on each module (this method should have been used for a long time) and finally found that something went wrong with the monster. Then, take a closer look and find that the strange path is an endless loop, that is, looking for the path to the Quartet until it is found. If there is no way to the four sides (stuck in by other monsters), the endless loop will continue. Finally, the whole thread is stuck.
Haha, finally solved.
There are two points to note.
1. I can't find the problem. I can comment out each module in sequence to see which module has the problem. I will find the lesions in detail.
2. If something goes wrong, do not think about the system. In addition, system-level errors are less likely.
3. If the thread is not running in the future, first consider whether there is an endless loop. Of course, it is not good to update everything in the game in a thread? The whole thread stops because of a dead location. The speed in one place will affect the whole thread, just like a series circuit.