What is a Condition Variable in java?

前端 未结 5 1745
时光取名叫无心
时光取名叫无心 2020-12-29 16:05

Q1. What is a condVar in Java? If I see the code below, does a condition variable necessarily have to be within the \'mutex.acquire()\' and

5条回答
  •  情话喂你
    2020-12-29 17:05

    Q1. I believe by "condition variable", you're referring to something you check to determine the condition that you waited on. For example - if you have the typical producer-consumer situation, you might implement it as something like:

    List list;
    
    public T get()
    {
        synchronized (list)
        {
             if (list.get(0) == null)
             {
                 list.wait();
             }
             return list.get(0);
        }
    }
    
    public void put(T obj)
    {
        synchronized (list)
        {
             list.add(obj);
             list.notify();
        }
    }
    

    However, due to the potential of spurious thread wakeups, it is possible for the consumer method to come out of the wait() call while the list is still empty. Thus it's good practice to use a condition variable to wait/sleep/etc. until the condition is true:

    while (list.get(0) == null)
    {
        list.wait();
    }
    

    using while instead of if means that the consumer method will only exit that block if it definitely has something to return. Broadly speaking, any sleep or wait or blocking call that was triggered by a condition, and where you expect the condition to change, should be in a while block that checks that condition every loop.

    In your situation you're already doing this with the while (count == array.length) wrapper around notFull.await().

    Q2. Timed wait is generally a good practice - the timeout allows you to periodically perform a sanity check on your environment (e.g. has a shutdown-type flag been flipped), whereas a non-timed wait can only be stopped by interruption. On the other hand, if the wait is going to just keep blocking anyway until the condition is true, it makes little difference it it wakes up every 50 ms (say) until the notify() happens 2 seconds later, or if it just blocks constantly for those 2 seconds.

    As for wait() vs sleep() - the former is generally preferable, since it means you get woken up as soon as you are able to take action. Thread.sleep(500) means that this thread is definitely not doing anything for the next 500ms, even if the thing it's waiting for is ready 2ms later. obj.wait(500) on the other hand would have been woken up 2ms into its sleep and can continue processing. Since sleeps introduce unconditional delays in your program, they're generally a clunkier way to do anything - they're only suitable when you're not waiting on any specific condition but actually want to sleep for a given time (e.g. a cleanup thread that fires every 60 seconds). If you're "sleeping" because you're waiting for some other thread to do something first, use a wait() (or other synchronous technique such as a CountDownLatch) instead.

    Q3. Pass - it looks like there's a lot of boilerplate there, and since the code doesn't have any comments in and you haven't explained what it's supposed to do and how it's meant to behave, I'm not going to try and reverse-engineer that from what you're written. ;-)

提交回复
热议问题