A Java multithreading problem recently encountered

Source: Internet
Author: User
Document directory
  • 1. Problem Description
  • 2. Solution
  • 3. Final background
1. Problem Description

My code has a multithreading problem with the following error:

My colleagues pointed out that the problem lies in multi-threaded data access. The problem is as follows: there are two main threads in the project: uithread in Android and glsurfaceview. glthread is responsible for rendering all the graphic elements in the scenario, the two threads simultaneously access data in the object.

The sample code is as follows:

Class gllinesgroupoverlay extends gllinesoverlay {protected arraylist <gllinesoverlay> mlinegroup; // call in uithreadpublic void populate () {synchronized (mlinegroup) {// initialization data} // call in uithreadpublic Boolean ONTAP (float X, float y) {synchronized (mlinegroup) {// Click Event Processing} // call in glthreadpublic void draw (gl10 GL) {synchronized (mlinegroup) {// drawing} class glmultipleaaoverlay extends gllinesgroupoverlay {@ overridepublic void populate () {// #### operate mlinegroup and forget to lock it #####}}

The operations on data members in the base class are all locked. When writing a derived class, the mind is not very clear. The method is rewritten but the lock is forgotten. In addition, the construction and call of glmultipleaaoverlay are also amazing, the Code is as follows:

// The overlay construction is called in the UI thread and added to the GL rendering queue. glmultipleaaoverlay mtoverlay = new glmultipleaaoverlay (); addoverlay (mtoverlay ); // Add to the GL rendering queue // ##### timing ###### mtoverlay. populate (); // initialize data.

The new mtoverlay object is immediately added to the rendering queue. After the addoverlay (mtoverlay) function returns, the glthread thread continuously calls the draw function of the overlay and then the UI thread calls mtoverlay. the result of the populate () function is miserable,Random crash,In addition, the behavior on different mobile phones is inconsistent. I have never crash on HTC G10, and the frequency on my colleagues' mobile phones is very high.

If the call code is changed:

// Overlay construction is called in the UI thread. After populate is complete, glmultipleaaoverlay mtoverlay = new glmultipleaaoverlay (); mtoverlay. populate (); // initialize data. Addoverlay (mtoverlay); // Add it to the GL rendering queue

After populate is called, the GL rendering starts and no concurrent access problem occurs. However, there is an extra pitfall:If glmultipleaaoverlay overrides the ONTAP function and forgets to lock the function, concurrent access will occur..

Multithreading is independent of the time series of function calls. Therefore, we cannot pin all the functions of gllinesgroupoverlay in a correct time series, the root cause of Multithreading is that the base class function can be rewritten in the derived class, And the mlinegroup of any data member can be accessed.

2. Solution

I thought of two ideas:

Method 1: mlinegroup is private. Instead of data protected, mlinegroup is a final protected data access interface, and mlinegroup is locked in the interface. But the granularity is too small.

Method 2:

Class extends gllinesoverlay {protected arraylist <gllinesoverlay> mlinegroup; // public final interface function // call in uithreadpublic final void populate () {synchronized (mlinegroup) {// initialize data populateimpl () ;}// call in uithreadpublic final Boolean ONTAP (float X, float y) {synchronized (mlinegroup) {ontapimpl (X, y) ;}// call in glthreadpublic final void draw (gl10 GL) {Synchro Nized (mlinegroup) {drawimpl (GL) ;}// protected function implementation // ##### these functions can be rewritten, but it cannot be locked ##### proteced void populateimpl () {// Operation Array, initialization data} protected Boolean ontapimpl (float X, float y) {// Operation Array, click Event Processing} protected Boolean void drawimpl (gl10 GL) {// Operation Array, drawing} class glmultipleaaoverlay extends {@ overrideprotected void populateimpl (){//~~~~~ Operation of mlinegroup, no locks required ~~~~~}}

The public final function is used as the shelf, and the protected overload function is implemented. The multi-threaded access is taken into account on the shelf, so you can directly implement the logic.
Using final to prevent the public interface from being overwritten, the protected function can rewrite the interface, and it cannot be locked if it is not necessary (if someone is full, it may be okay to lock it )!

The data is set to protected. You want to derive the data to access and modify the data, and want the derived class to access the data with a lock.I really don't have a solution. I can only hope that the people who write the derived classes have a clear mind!

3. The final post-language Java data does not want to be modified, and the function does not want to be rewritten to adopt final.
The final class cannot be inherited and has no subclass. The methods in the final class are final by default.
The final method cannot be overwritten by the quilt class, but can be inherited.
Final member variables indicate constants and can only be assigned once. The values do not change after the values are assigned.
Final cannot be used to modify the constructor. C ++ data doesn't want to be modified const; C ++ 11 begins to increase override and final keywords: http://www.devbean.net/2012/05/cpp11-override-final/

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.