How to do it...

Follow these steps to implement the example:

  1. Create a class named Operations:
        public class Operations {
  1. Implement a public static method named readData(). It puts the current thread to sleep for 500 milliseconds:
        public static void readData(){ 
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Implement a public static method named writeData(). It puts the current thread to sleep for 500 milliseconds:
        public static void writeData(){ 
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Implement a public static method named processData(). It puts the current thread to sleep for 2,000 milliseconds:
        public static void processData(){ 
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Implement a class named Task1 and specify that it implements the Runnable interface:
        public class Task1 implements Runnable {
  1. Declare a private Lock attribute named lock:
        private final Lock lock;
  1. Implement the constructor of the class to initialize its attributes:
        public Task1 (Lock lock) { 
this.lock=lock;
}
  1. Implement the run() method. Acquire the lock, call the three operations of the Operations class, and release the lock:
        @Override 
public void run() {
lock.lock();
Operations.readData();
Operations.processData();
Operations.writeData();
lock.unlock();
}
  1. Implement a class named Task2 and specify that it implements the Runnable interface:
        public class Task2 implements Runnable {
  1. Declare a private Lock attribute named lock:
        private final Lock lock;
  1. Implement the constructor of the class to initialize its attributes:
        public Task2 (Lock lock) { 
this.lock=lock;
}
  1. Implement the run() method. Acquire the lock, call the readData() operation, and release the lock. Then, call the processData() method, acquire the lock, call the writeData() operation, and release the lock:
        @Override 
public void run() {
lock.lock();
Operations.readData();
lock.unlock();
Operations.processData();
lock.lock();
Operations.writeData();
lock.unlock();
}
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main { 

public static void main(String[] args) {
  1. Create a Lock object named lock, a Task1 object named task1, a Task2 object named task2, and an array of 10 threads:
        Lock lock=new ReentrantLock(); 
Task1 task1=new Task1(lock);
Task2 task2=new Task2(lock);
Thread threads[]=new Thread[10];
  1. Launch 10 threads to execute the first task by controlling its execution time:
        Date begin, end; 

begin=new Date();
for (int i=0; i<threads.length; i++) {
threads[i]=new Thread(task1);
threads[i].start();
}

for (int i=0; i<threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
end=new Date();
System.out.printf("Main: First Approach: %d ",
(end.getTime()-begin.getTime()));
  1. Launch 10 threads to execute the second task by controlling its execution time:
        begin=new Date(); 
for (int i=0; i<threads.length; i++) {
threads[i]=new Thread(task2);
threads[i].start();
}

for (int i=0; i<threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
end=new Date();
System.out.printf("Main: Second Approach: %d ",
(end.getTime()-begin.getTime()));
..................Content has been hidden....................

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