简体   繁体   English

我可以调用一个同步方法,该方法调用一个调用同步方法的非同步方法吗?

[英]Can I call a synchronized method that calls a non-synchronized method that calls a synchronized method?

In Java using the synchronized keyword all within a single object and thread. 在Java中,使用synced关键字都在单个对象和线程中。

Can I call a synchronized method that calls a non-synchronized method that calls a synchronized method, without the final synchronized method blocking for the first synchronized method to be completed? 我可以调用一个同步方法,该方法调用一个非同步方法,该非同步方法调用一个同步方法,而最终同步方法不会阻塞第一个同步方法的完成吗?

With a single object and a single thread, there's no problem. 有了一个对象和一个线程,就没有问题。

Your only thread is able to acquire all the locks, and due to re-entrancy, it can acquire them several times. 您唯一的线程能够获取所有锁,并且由于重新进入,它可以获取多次。

Even if we add another thread, it'll work (with one object). 即使我们添加另一个线程,它也将起作用(使用一个对象)。 One thread will acquire the first lock, blocking the second thread, and execution continues normally. 一个线程将获得第一个锁,阻塞第二个线程,并且执行将继续正常进行。

With multiple threads and multiple objects, the answer is "it depends on how the code is written". 对于多个线程和多个对象,答案是“这取决于代码的编写方式”。

Your code can always call a method whether it is synchronized or not. 您的代码始终可以调用一个方法,无论它是否同步。 A better question is, what will happen when your code calls it. 一个更好的问题是,当您的代码调用它时会发生什么。

A synchronized method that looks like this: 如下所示的同步方法:

synchronized void foobar() {
    doSomething();
}

Actually is just a short-hand way of writing this: 实际上,这只是编写此代码的一种简便方法:

void foobar() {
    synchronized(this) {
        doSomething();
    }
}

So any question about calling a synchronized method really is a question about executing a synchronized block . 因此,有关调用同步方法的任何问题实际上就是有关执行同步块的问题 There are three things that could happen when your code enters synchronized(this) {...} . 当您的代码进入synchronized(this) {...}时,可能会发生三件事。

1) If the this object is not locked, then it will be locked in the name of the calling thread, the thread will execute the statements in the block, and then it will unlock the lock when it's done. 1)如果未锁定this对象,则它将以调用线程的名称锁定,该线程将执行块中的语句,然后在完成锁定后将其解锁。

2) If the this object already has been locked by the calling thread, then the thread will simply execute the statements in the block. 2)如果this对象已被调用线程锁定,则该线程将仅执行该块中的语句。

3) If the this object is locked by some other thread, then the calling thread will wait until the other thread unlocks it, and then it will proceed as in case (1). 3)如果this对象被某个其他线程锁定,则调用线程将等待,直到另一个线程将其解锁为止,然后它将像情况(1)一样继续进行。

The only way you can get into trouble is if your code attempts to lock two different locks. 麻烦的唯一方法是代码尝试锁定两个不同的锁。 Then, if your design is not well thought out, two or more threads could deadlock , which is something you can read about elsewhere. 然后,如果您的设计没有经过深思熟虑,则两个或多个线程可能会死锁 ,这是您可以在其他地方阅读的内容。

If a class has both synchronized and non-synchronized methods, multiple threads can still access the class's non-synchronized methods.If you have methods that don't access the data you're trying to protect, then you don't need to synchronize them. 如果一个类同时具有同步方法和非同步方法,则多个线程仍可以访问该类的非同步方法。如果您的方法不访问您要保护的数据,则不需要同步他们。 Synchronization can cause a hit in some cases (or even deadlock if used incorrectly) 在某些情况下,同步可能会导致命中(如果使用不正确,甚至会导致死锁)

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

相关问题 如果一个同步方法调用另一个非同步方法,非同步方法是否有锁 - If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method 是否可以将非同步方法称为已同步? - Is it possible to call a non-synchronized method as synchronized? 在同一个类中执行同步方法和非同步方法 - Execution on synchronized and non-synchronized method in the same class 从非同步方法内部调用同步 - Calling a synchronized from inside a non-synchronized method 非同步方法可以被其他线程访问 - non-synchronized method can access by other thread,,,,,,, 从java中的非同步方法调用synchronized时会发生什么 - what happens when you call a synchronized from a non-synchronized method in java 非同步方法可以与同步方法交错吗? - Can a non synchronized method interleave with a synchronized method? 如果我从我的synchronized方法调用非同步方法是非同步方法线程安全吗? - if i call a non synchronized method from my synchronized method is non synchronized method thread safe? 同步方法调用另一个调用wait()的同步方法 - Synchronized method calling another synchronized method which calls wait() 调用同步方法时线程调用非同步实例方法 - Thread calling non-synchronized instance method when a synchronized method is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM