zoukankan      html  css  js  c++  java
  • intrinsicConditionQueue笔记

    一, 使用conditionQueue需要注意的一些点:

      一个conditionQueue被多种Predicate condition 使用是很正常的,所以当一个wait的线程被唤醒的时候,很有可能它监听的条件并没有成功,所以在wait醒后,最好再次进行条件的判断.

       一定要在wait前就对条件进行判断,因为如果一个条件已经是true了,然后再进行wait,很可能会导致Missed Signals,   

       调用notify()比较危险,因为存在一个conditionQueue被多种Predicate condition 使用的情况,所以使用notify()可能会导致Missed Signals同样的问题,所以最好使用notifyAll() 这种的问题是,每次都notifyAll()可能会有过多的线程切换,会耗费性能

    For all these reasons, when you wake up from  wait  you must test the condition predicate again, and go back to waiting
    (or fail) if it is not yet true. Since you can wake up repeatedly  without your condition predicate being true, you must
    therefore always call  wait  from within a loop, testing the condition predicate in each iteration. The canonical form for a
    condition wait is shown in Listing 14.7.
    Listing 14.7. Canonical Form for State­dependent Methods.
    void stateDependentMethod() throws InterruptedException {
    // condition predicate must be guarded by lock
    synchronized(lock) {
    while (!conditionPredicate())
    lock.wait();
    // object is now in desired state
    }
    }

    When using condition waits ( Object.wait  or  Condition.await ):
    • Always have a condition predicatesome test of object state that must hold before proceeding;
    • Always test the condition predicate before calling  wait , and again after returning from  wait ;
    • Always call  wait  in a loop;
    • Ensure  that  the  state  variables  making  up  the  condition  predicate  are  guarded  by  the  lock  associated  with  the
    condition queue;
    • Hold the lock associated with the the condition queue when calling  wait ,  notify , or  notifyAll ; and
    • Do not release the lock after checking the condition predicate but before acting on it.

    Single  notify  can be used instead of  notifyAll  only when both of the following conditions hold:
    • Uniform waiters. Only one condition predicate is associated with the condition queue, and each thread executes the
    same logic upon returning from  wait ; and
    • One‐in, one‐out. A notification on the condition variable enables at most one thread to proceed.

  • 相关阅读:
    整数的唯一分解定理【模板】
    poj 1419Graph Coloring 【dfs+补图+计算最大团+计算最大独立集 【模板】】
    poj 1065 Wooden Sticks 【贪心 新思维】
    poj 3126 Prime Path 【bfs】
    hihocoder 第五十二周 高斯消元·二【高斯消元解异或方程 难点【模板】】
    poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】
    poj 1019 Number Sequence 【组合数学+数字x的位宽函数】
    括号匹配
    Digit
    Ace of Aces
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/7643286.html
Copyright © 2011-2022 走看看