简体   繁体   English

同步方法中的等待与不等待(java)

[英]Wait vs No wait in Synchronized methods (java)

Hi I'm pretty new to Java and now I'm getting into java concurrency.嗨,我对 Java 还很陌生,现在我正在接触 Java 并发。 And I have a little doubt about Synchronized methods: i have seen that I can get the same results using an If else inside a Synchronized method, checking every time If the condition to do an action is fullfilled, as using a wait / notify approach.而且我对同步方法有点怀疑:我已经看到我可以使用同步方法中的 If else 获得相同的结果,每次检查是否满足执行操作的条件,就像使用等待/通知方法一样。

Since i get the same result I'm wondering If the If else approach has any advantages or disadvantages over t'he wait and notify approach?由于我得到了相同的结果,我想知道 If else 方法是否比等待和通知方法有任何优点或缺点? I supose that efficiency will be a disadvantage, since If is always checking the condition, Who le wait Just stops and waits for notify.我认为效率将是一个劣势,因为 If 总是检查条件,Who le wait 只是停止并等待通知。 But are any other advantages or disadvantages?但是还有其他优点或缺点吗?

Thx!谢谢!

You are mixing two concepts.您正在混合两个概念。 If-Else vs Wait-Notify are totally different. If-Else 和 Wait-Notify 是完全不同的。 You want two threads to communicate with each-other that is where Wait-Notify would be used while if-else is general conditional statement.你想两个线程,每个-其他那就是等待通知将被使用,而如果其他是通用的条件语句沟通。

You cannot have two threads communicate with each other simply using if-else condition.不能简单地使用 if-else 条件让两个线程相互通信。 You can write your code that makes it look like it does however you are simply not allowing threads to interact with each other.您可以编写代码使其看起来像这样,但是您只是不允许线程相互交互。

Moreover it can lead to undesirable consequences/computational states.此外,它可能导致不良后果/计算状态。 Sooner or later you would have hotchpotch code.迟早你会有 hotchpotch 代码。

synchronized block makes the code thread safe.同步块使代码线程安全。 You would want to use wait() and notify() or notifyAll() if you want to be more efficient.如果您想提高效率,您可能需要使用wait()notify()notifyAll()

For example if your shared resource is a list, multiple threads share.例如,如果您的共享资源是一个列表,则多个线程共享。 If you put it in synchronized block of a monitor then threads will constantly jump in and run the code, during context switches.如果将它放在监视器的同步块中,则线程将在上下文切换期间不断跳入并运行代码。 Even if the list is enpty!!即使列表是空的!!

The wait() is hence used on the monitor (the object inside the synchronized(..) ) as a mechanism to 'tell' all threads to chill out and stop using CPU cycles until further notice or notifyAll() .因此, wait()用于监视器( synchronized(..)的对象)作为一种“告诉”所有线程冷静下来并停止使用 CPU 周期的机制,直到进一步通知或notifyAll()

synchronized(monitor) {

    while( list.isEmpty() ) 
       monitor.wait();
    doSomething(...)
}

In the above example, doSomething() will be executed only when the list is not empty, after another thread executed notify() or notifyAll() somewhere else in the code.在上面的例子中,只有当列表不为空时, doSomething()才会被执行,在另一个线程在代码中的其他地方执行了notify()notifyAll()

BUT with the following code:但是使用以下代码:

synchronized(monitor) {

    if(!list.isEmpty())
        doSomething(...)
}

When a thread comes in to the synchronized block, there are 3 possible scenarios:当一个线程进入同步块时,有 3 种可能的情况:

  1. The list is empty: doSomething() will not be executed.列表为空: doSomething()不会被执行。
  2. The list is NOT empty: doSomething() may be executed properly, or...该列表不为空: doSomething()可能被正确执行,或者...
  3. If there was a context switch right after the if and before doSomething , and the other thread got all list's items out, after another context-switch out thread will execute doSomethig() on an empty list.如果在ifdoSomething之前有一个上下文切换,并且另一个线程将所有列表的项目都取出来,那么在另一个上下文切换出线程之后,将在一个空列表上执行doSomethig()

So, just to sum everything up, if you use wait/notify, you guarantee more efficient code!所以,总结一下,如果你使用等待/通知,你保证更高效的代码! thread will not work when they don't need to.当他们不需要时,线程将不起作用。

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

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