Can you use the synchronized keyword inside a Lambda expression?

Yes, a lambda expression can contain a synchronized block, but the synchronized keyword cannot be applied to the lambda itself the way it can to a method — you must synchronize explicitly on an object inside the lambda body.

Key Points

Example

A Runnable lambda that increments a shared counter could synchronize on a dedicated lock object: () -> { synchronized (lock) { counter++; } }.

private final Object lock = new Object();
private int counter = 0;

Runnable increment = () -> {
    synchronized (lock) {
        counter++;
    }
};

Interview Tip

A concise interview answer is

You can't put the synchronized keyword directly on a lambda the way you would on a method, because a lambda has no implicit lock object. Instead, I put a synchronized block inside the lambda body around an explicit lock object captured from the enclosing scope, or better, I reach for a concurrency utility like AtomicInteger or ConcurrentHashMap when possible.

Last updated: 16 July 2026

Previous question: What will happen if you try to modify a local variable inside a Lambda expression?

Next question: What is the difference between count(), sum(), and reduce() in Java 8 Streams?

More Java 8 interview questions

All Java 8 questions