• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

tryLock()

What is ReentrantLock?

May 4, 2017 By j2eereference Leave a Comment

ReentrantLock is an implementation of Lock interface and comes under java.util.concurrent.locks. ReentrantLock concept is same as traditional synchronized  methods but it has more capability like fairness approach,Lock interruptibly ,tryLock and other features which we will discuss under important method of ReentrantLock section.

Constructors:

ReentrantLock() : this constructor is used to create instance of ReentrantLock which means fair value is false .

ReentrantLock(boolean fair): this construct will create instance of ReentrantLock which accept Boolean value as parameter either true or false.

If boolean variable fair is true it means that thread which is waiting from long will get the lock first

If boolean variable flag is false then it means any thread can get the lock .

Example:

Java
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class ReentrantLockDemo {
  public static void main(String[] args) {
   Lock lock = new ReentrantLock();
   WorkerThread workerThread = new WorkerThread(lock);
   new Thread(workerThread, "WorkerThread1").start();
   new Thread(workerThread, "WorkerThread2").start();
   new Thread(workerThread, "WorkerThread3").start();
}
}

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class WorkerThread implements Runnable {
   Lock lock;
   public WorkerThread(Lock lock) {
   this.lock = lock;
}
 
  public void run() {
   lock.lock();
   System.out.println("Lock has been acquired by "+ Thread.currentThread().getName());
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   System.out.println("Lock has been released by "+ Thread.currentThread().getName());
   lock.unlock();
  }
}

Output:
Lock has been acquired by WorkerThread1
Lock has been released by WorkerThread1
Lock has been acquired by WorkerThread2
Lock has been released by WorkerThread2
Lock has been acquired by WorkerThread3
Lock has been released by WorkerThread3

Important Methods:

1) void lock() : This method will cause current thread to hold the lock if it is not acquired by other thread otherwise current thread will wait until acquired lock gets released by other thread.
One more scenario is that lock hold count would increased by 1 in case lock is already held by current thread
2)void unlock(): If current thread calls unlock method then lock is released if lock hold count is zero and if it greater than 0 then lock hold count get decremented by 1.
3)boolean tryLock(): This method will acquires lock and return true if lock is not held by any thread otherwise returns false.
4)boolean tryLock(long timeout, TimeUnit unit):This method will wait for the lock to become available until time mentioned in the method timeouts.
5)void lockInterruptibly():This method allows the thread to immediately react to the interrupt signal sent by other thread and throw interrupted exception.
6)getHoldCount() :This method will give you hold count on this lock by the current thread.
7)isHeldByCurrentThread() :This method will check whether current thread holds the lock .

Related Posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof

Filed Under: concurrent package, Core Java Tagged With: concurrent API, Lock Interface, Reentrant Lock, tryLock()

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.