Entrance Exams
Govt. Exams
Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.
When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.
The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.
A deadlock occurs when two or more threads are blocked forever, waiting for each other to release resources they need.
Both extending Thread class and implementing Runnable interface are valid approaches to create threads in Java.
A daemon thread runs in the background and doesn't prevent the JVM from exiting. When all non-daemon threads finish, the JVM exits even if daemon threads are running.
wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.
The AtomicInteger class (and other Atomic classes) provides thread-safe operations without explicit synchronization using low-level atomic operations.
notify() wakes up a single thread that is waiting on the object's monitor, while notifyAll() wakes up all threads waiting on that object.
A synchronized method locks the object, allowing only one thread to execute it at a time for that object instance.