Getting exclusive system-wide lock in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Standard Java synchronization mechanisms (synchronized, ReentrantLock) only work within a single JVM. For system-wide locking across multiple JVM processes on the same machine, Java provides file-based locking via java.nio.channels.FileLock. For locking across multiple machines, use distributed lock managers like database row locks, Redis (Redisson), or ZooKeeper. The most common approach for single-machine cross-process locking is FileLock on a shared lock file.
In-JVM Locks (Not System-Wide)
These mechanisms coordinate threads within a single process. Two separate Java applications running on the same machine cannot share these locks.
FileLock for System-Wide Locking
channel.lock() acquires an exclusive lock on the file. If another process already holds the lock, this call blocks until the lock is released. The OS enforces the lock across all processes.
Non-Blocking tryLock
tryLock() returns null instead of blocking, which is useful for ensuring only one instance of an application runs at a time.
Reusable Lock Utility
Server Socket as Single-Instance Lock
Binding a ServerSocket to a localhost port is a simple alternative to file locks. If the port is already bound, another instance is running. The OS automatically releases the port when the process exits.
Distributed Locks (Multi-Machine)
Common Pitfalls
- Assuming
synchronizedworks across JVM processes:synchronizedandReentrantLockonly coordinate threads within a single JVM. Two separate Java applications cannot share these locks. UseFileLockor a distributed lock for cross-process coordination. - Not releasing
FileLockin a finally block: If an exception occurs betweenlock()andrelease(), the lock is held until the process exits. Always release in afinallyblock or use try-with-resources. - FileLock behavior varies by OS: On Linux,
FileLockis advisory — non-Java processes can ignore it. On Windows, it is mandatory (enforced by the OS). Do not rely onFileLockto prevent access from non-cooperating processes on Linux. - Forgetting lock expiration in distributed locks: If a process crashes while holding a Redis or database lock, the lock may never be released. Use lock timeouts (TTL) so that stale locks expire automatically.
- Using file locks on NFS-mounted filesystems: NFS file locking is unreliable and varies by implementation. If processes run on different machines, use a proper distributed lock (Redis, ZooKeeper, database) instead of file locks on shared storage.
Summary
synchronizedandReentrantLockwork within a single JVM only — not across processes- Use
java.nio.channels.FileLockfor system-wide locking on a single machine channel.lock()blocks until the lock is available;channel.tryLock()returnsnullimmediately if unavailable- Binding a
ServerSocketto a localhost port is a simple single-instance check - For multi-machine distributed locks, use Redis (Redisson), ZooKeeper, or database row locks with
SELECT ... FOR UPDATE - Always release locks in
finallyblocks and use timeouts for distributed locks to prevent deadlocks from crashed processes
Related reading
- Getting “INTERNAL” exception when an async Firebase function is called from android app
- Getting return value from Task.Run
- Getting the thread ID from a thread
- Getting the thread ID from a thread
- Getting Gradle dependencies in IntelliJ IDEA using Gradle build
- Getting hold of the outer class object from the inner class object
- Getting thread id of current method call
- Given that HashMaps in jdk1.6 and above cause problems with multithreading, how should I fix my code

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.