Confusing the use of synchronization in Java: patterns or antipatterns?

I'm doing a code review of changes to Java products I don't own I am not a Java expert, but I strongly doubt that this is meaningless and shows a fundamental misunderstanding of how synchronization works

synchronized (this) {
    this.notify();
}

But I may be wrong, because Java is not my main playground There may be a reason to do so I would appreciate it if you could enlighten me

Solution

This is certainly not meaningless. You can have another thread refer to the object containing the above code

synchronized(foo) {
    foo.wait();
}

To be awakened when something happens Although in many cases, synchronization on internal / private locked objects rather than this is considered a good practice

However, only one can be done within the synchronization block Notify () can be very wrong - you usually need to do some work and notify under normal circumstances. Under normal circumstances, you also need to atomize other threads We must see more code to determine if it is really wrong

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>