Last Updated:

Java Lock API | Theory and example of use

In most cases, the keyword is a good choice, but still has some drawbacks. That is why java 1.5 introduced a package with an interface and some additional classes that improved the locking mechanism.synchronizedConcurrency APIjava.util.concurrent.locksLock

Important points in the Concurrency Lock API

  1. Lock: This is the basic interface in the . It provides all the functions of the keyword, adding new methods for convenient work. For example:Lock APIsynchronized
  • the method is used in order to get a lock to work;lock()
  • method — release lock;unlock()
  • a method for waiting for a lock for a certain time;tryLock()
  • the method is to create, etc.newCondition()Condition
  1. ConditionA: It's like a model with a number of additional features. An object is always created by using the . Such an important method as very similar to , and the methods are similar to and .wait-notifyConditionLockawait()wait()signal()signalAll()notify()notifyAll()
  2. ReadWriteLock contains a pair of related locks: the first is read-only, the second is writable. A read lock can provide access to multiple threads at the same time.
  3. A class is the most used implementation of the . This implementation of the interface is similar to the use of the . In addition to implementing the interface, it contains a number of helper methods for working with threads. ReentrantLockLockLocksynchronizedLockReentrantLock

Let's look at the use of a small program as an example:Java Lock API

Let's say we have a test class with synchronized methods for processing something.

If the thread is included in the method , then a lock occurs on the Test object. When a thread tries to execute a method, it executes it unhindered because it is already drawing a Test object. This is exactly similar to using the .foo()bar()synchronized(this)

And now let's look at a simple example where you can and should replace the use of the keyword with .synchronizedLock API

And so, let's have a Resource class with a couple of thread-safe methods and methods where thread security is not required.

Now take a class that implements the Runnable interface and uses the methods of the .Resource

Note that we use the access block to get the lock on the Resource object.synchronized

Now let's rewrite the above program using the keyword instead.Lock APIsynchronized

As you can see from the program, we use the method to make sure that the thread is only waiting for a certain amount of time. If it doesn't get a lock on the object, it just logs and exits.tryLock()

Java Lock API

Another important point. We use a block to make sure that the lock is lifted even if the method throws an exception.try-finallydoSomething()

Advantages and disadvantages of each of the methods or Lock vs synchronized

Based on the above information and a simple example of use and block, we can draw the following conclusions about the advantages and disadvantages of each of the methods or simply point out the difference between them.Lock APIsynchronized

  1. Lock API provides more possibilities for locking, as opposed to , where a stream can endlessly wait for a lock. In, we can use the method to wait for the lock only for a certain time.synchronizedLock APItryLock()
  2. Synchronized code is much cleaner and easier to maintain. In the case of use, we have to write a block to make sure that the lock will be lifted, even if between method calls and .Lock APItry-finallylock()unlock()
  3. Synchronization blocks can cover only one method, while allowing you to get a lock in one method and remove it in another.Lock API

That's all you need to know about the Lock API, its advantages and disadvantages over the block, in order to write simple thread-safe programs in Java. Read more about multithreading and parallelism in a separate section of the site.synchronized