How to do it...

Follow these steps to implement the example:

  1. Create a class named MyLock that extends the ReentrantLock class:
        public class MyLock extends ReentrantLock {
  1. 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();
}
  1. 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();
}
  1. Create a class named Task that implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private Lock attribute named lock:
        private final Lock lock;
  1. Implement a constructor of the class to initialize its attribute:
        public Task (Lock lock) { 
this.lock=lock;
}
  1. Implement the run() method. Create a loop with five steps:
        @Override 
public void run() {
for (int i=0; i<5; i++) {
  1. Acquire the lock using the lock() method and print a message:
        lock.lock(); 
System.out.printf("%s: Get the Lock. ",
Thread.currentThread().getName());
  1. 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();
}
}
}
}
  1. 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 {
  1. Create a MyLock object named lock:
        MyLock lock=new MyLock();
  1. Create an array of five Thread objects:
        Thread threads[]=new Thread[5];
  1. 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();
}
  1. Create a loop with 15 steps:
        for (int i=0; i<15; i++) {
  1. 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());
  1. 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(" ");
}
  1. 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("************************ ");
  1. Put the thread to sleep for 1 second and close the loop and the class:
              TimeUnit.SECONDS.sleep(1); 
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.183