简体   繁体   English

Java(Android)旧版代码中的线程同步

[英]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. 此AST元素表示一个“监视”语句。 It can be one of two types: 它可以是以下两种类型之一:

__monitor_enter(lock)
__monitor_exit(lock)

Such statements are not legal Java constructs. 此类声明不是合法的Java构造。 Combined with try-finally blocks, they are used to represent very high-level constructs known as synchronized blocks. 与try-finally块结合使用,它们用于表示称为同步块的非常高级的结构。

Currently, monitors statements are read-only elements and cannot be user-created. 当前,monitors语句是只读元素,不能由用户创建。

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. 在某些地方__monitor_enter给出了错误,而在某些地方没有出错,这可能是原因所在,如果有人理解了代码的风格,请告诉您这种声明的目的是什么,以及我如何改进它。

Edit: 编辑:

the error is 错误是

The method __monitor_exit(Object) is undefined for the type MyApplication 对于类型MyApplication,未定义方法__monitor_exit(Object)

and

The method __monitor_enter(Object) is undefined for the type MyApplication 未定义类型MyApplication的方法__monitor_enter(Object)

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. 检查它可以正常编译的情况,并查看在那里使用了哪种类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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