Follow these steps to implement the example:
- Create a class named MyLock that extends the ReentrantLock class:
public class MyLock extends ReentrantLock {
- Implement getOwnerName(). This method returns the name of the thread that has control of a lock (if any), using the protected method of the Lock class called getOwner():
public String getOwnerName() {
if (this.getOwner()==null) {
return "None";
}
return this.getOwner().getName();
}
- Implement getThreads(). This method returns a list of threads queued in a lock, using the protected method of the Lock class called getQueuedThreads():
public Collection<Thread> getThreads() {
return this.getQueuedThreads();
}
- Create a class named Task that implements the Runnable interface:
public class Task implements Runnable {
- Declare a private Lock attribute named lock:
private final Lock lock;
- Implement a constructor of the class to initialize its attribute:
public Task (Lock lock) {
this.lock=lock;
}
- Implement the run() method. Create a loop with five steps:
@Override
public void run() {
for (int i=0; i<5; i++) {
- Acquire the lock using the lock() method and print a message:
lock.lock();
System.out.printf("%s: Get the Lock. ",
Thread.currentThread().getName());
- Put the thread to sleep for 500 milliseconds. Free the lock using the unlock() method and print a message:
try {
TimeUnit.MILLISECONDS.sleep(500);
System.out.printf("%s: Free the Lock. ",
Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
- Create the main class of the example by creating a class named Main with a main() method:
public class Main {
public static void main(String[] args) throws Exception {
- Create a MyLock object named lock:
MyLock lock=new MyLock();
- Create an array of five Thread objects:
Thread threads[]=new Thread[5];
- Create and start five threads to execute five Task objects:
for (int i=0; i<5; i++) {
Task task=new Task(lock);
threads[i]=new Thread(task);
threads[i].start();
}
- Create a loop with 15 steps:
for (int i=0; i<15; i++) {
- Write the name of the owner of the lock in the console:
System.out.printf("Main: Logging the Lock ");
System.out.printf("************************ ");
System.out.printf("Lock: Owner : %s ",lock.getOwnerName());
- Display the number and name of the threads queued for the lock:
System.out.printf("Lock: Queued Threads: %s ",
lock.hasQueuedThreads());
if (lock.hasQueuedThreads()){
System.out.printf("Lock: Queue Length: %d ",
lock.getQueueLength());
System.out.printf("Lock: Queued Threads: ");
Collection<Thread> lockedThreads=lock.getThreads();
for (Thread lockedThread : lockedThreads) {
System.out.printf("%s ",lockedThread.getName());
}
System.out.printf(" ");
}
- Display information about the fairness and status of the Lock object:
System.out.printf("Lock: Fairness: %s ",lock.isFair());
System.out.printf("Lock: Locked: %s ",lock.isLocked());
System.out.printf("************************ ");
- Put the thread to sleep for 1 second and close the loop and the class:
TimeUnit.SECONDS.sleep(1);
}
}
}