IT lexicon › Programming › Synchronized

Synchronized

Programming På svenska → Updated: 2026-11-15

Java's built-in mechanism for exclusive access: mark a method or code block with synchronized, and Java ensures only one thread executes it at a time.

Java's synchronized keyword (JDK 1.0, 1995) was an attempt to make threading simple: decorate a method or block, and Java handles the locking for you. Behind the scenes, every Java object holds a mutex, and entering a synchronized block acquires it; leaving releases it. If another thread is blocked on the same lock, it waits. Simple and intuitive, but coarse-grained locks can lead to deadlock and poor scalability.

Modern Java code often uses finer-grained locks from java.util.concurrent (ReentrantLock, ReadWriteLock) for more control, but synchronized remains the right default for simple cases. The JVM can also optimize it away via lock elision when the compiler proves the lock is unused.

← Back to the lexicon