简体   繁体   中英

Thread Synchronization in Java(Android) Legacy Code

I have a legacy application to maintain and there is some code written like

public class MyApplication extends Application {
   ...(some code)

 Class cls = MyApplication.class;
        __monitor_enter(cls);

   ...(some code)

__monitor_exit(cls);

 ...(some code)
}

to my best of knowledge some sort of thread synchronization is going on here, but as explained here

This AST element represents a "monitor" statement. It can be one of two types:

__monitor_enter(lock)
__monitor_exit(lock)

Such statements are not legal Java constructs. Combined with try-finally blocks, they are used to represent very high-level constructs known as synchronized blocks.

Currently, monitors statements are read-only elements and cannot be user-created.

at some places the __monitor_enter is giving error and in some places its not, what probably could be the reason, if somebody understand the style of code please tell what is the purpose of such statement and how i can improve it.

Edit:

the error is

The method __monitor_exit(Object) is undefined for the type MyApplication

and

The method __monitor_enter(Object) is undefined for the type MyApplication

Regards.

Using a synchronization block:

public class MyApplication extends Application {
   ...(some code)

   synchronized (MyApplication.class) {    
      ...(some code)    
   }

   ...(some code)
}

As for the monitor, it seems that it can except as a locking object only a specific type of class/object. Check an occurrence where it compiles fine and see which type of object is used there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM