A disaster for beginners-the runnable run method is not called

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.