Lock interface in thread synchronization

               It is the built-in interface in java.util.concurrent.locks package.it is very easy to handle the synchronization in a flexible way. It has three types of locking such as interruptible locking, try-locking and timed locking.

Let us create a java program to illustrate the lock interface.

  • ·       This program starts from including the built-in package.
  • ·       A class is written as SResource.
  • ·       It creates a lock object as reentrantlock.
  • ·       A method “SafeMethod()” is act as critical region. It locks and unlocks the thread based on the situation.
  • ·       At last, the thread is unlocked using finally block.
  • ·       To get and display the output, the LockEg class is used.
  • ·       It creates the object for SResource. Using Runnable class, it creates a task to get the current thread to be run.
  • ·       4 different threads are created and started.
  • ·       Finally, the thread outputs are displayed.

Code:

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

class SResource {

    private final Lock l1 = new ReentrantLock();

    public void safeMethod(String tName) {

        l1.lock(); // is locks the thread

        try {

            System.out.println(tName + " is locked.");

            // Critical section part

            for (int i = 0; i < 4; i++) {

                System.out.println(tName + " is working: " + i);

                try {

                    Thread.sleep(300); // simulation part

                } catch (InterruptedException e) {

                    Thread.currentThread().interrupt();

                }

            }

        } finally {

            System.out.println(tName + " is released the lock.");

            l1.unlock(); // it releases the lock

        }

    }

}

public class LockEg {

    public static void main(String[] args) {

        SResource resrc = new SResource();

        Runnable task1 = () -> resrc.safeMethod(Thread.currentThread().getName());

        Thread t1 = new Thread(task1, "Thread-1");

        Thread t2 = new Thread(task1, "Thread-2");

        Thread t3 = new Thread(task1, "Thread-3");

        Thread t4 = new Thread(task1, "Thread-4");

        t1.start();

        t2.start();

        t3.start();

        t4.start();

    }

}

Output:

C:\raji\blog>javac LockEg.java

C:\raji\blog>java LockEg

Thread-1 is locked.

Thread-1 is working: 0

Thread-1 is working: 1

Thread-1 is working: 2

Thread-1 is working: 3

Thread-1 is released the lock.

Thread-2 is locked.

Thread-2 is working: 0

Thread-2 is working: 1

Thread-2 is working: 2

Thread-2 is working: 3

Thread-2 is released the lock.

Thread-3 is locked.

Thread-3 is working: 0

Thread-3 is working: 1

Thread-3 is working: 2

Thread-3 is working: 3

Thread-3 is released the lock.

Thread-4 is locked.

Thread-4 is working: 0

Thread-4 is working: 1

Thread-4 is working: 2

Thread-4 is working: 3

Thread-4 is released the lock.

This is the simple code illustrates the Lock interface in java. Hope, this coding sample is useful to you.  Keep coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.