Search code examples
jvmjava.util.concurrent

Does JVM know anything about java.util.concurrent package?


Below in try block, there are 3 statements must be executed in that order. Is there any possibility that statements could run out of order? Does JVM look ahead in j.u.c classes to see synchronization indicators (synchronized, volatile) and figure out it must not reorder the execution?

private Deque<Integer> deque = new LinkedList<Integer>();
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
class Producer implements Runnable {
    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                deque.add(1);
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }
}

Solution

  • The JVM will never reorder function calls unless it has fully inlined them. Any side effect at all can hide behind them, so unless it can prove that this is not so, it cannot reorder them.