Dakait
Dakait

Reputation: 2620

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.

Upvotes: 0

Views: 317

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53565

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.

Upvotes: 1

Related Questions