Program:- import java.util.concurrent.*; import java.util.concurrent.locks.*; class Display { ReentrantLock l=new ReentrantLock(); public void wish(String name) { l.lock(); for(int i=1;i<=5;i++){ System.out.print("Good morning: "); try{Thread.sleep(500);}catch(Exception e){} System.out.println(name); } l.unlock(); } } class ThreadDemo2 extends Thread { Display d; String name; ThreadDemo2(Display d, String name) { this.d=d; this.name=name; } public void run() { d.wish(name); } public static void main(String args[]) { Display d=new Display(); ...
What is the need for speed in the algorithm? Suppose there is the software that provides lots of features including security but it takes almost 30 min to perform one task. And on the other hand, there is the software that provides you the same feature but it takes 2 min to perform one task. which one would you prefer? This is the reason why we should design an algorithm that is faster. Given two algorithms for a task, how do we find out which one is better? One naive way of doing this is – implement both the algorithms and run the two programs on your computer for different inputs and see which one takes less time. There are many problems with this approach for the analysis of algorithms. 1) It might be possible that for some inputs, the first algorithm performs better than the second. And for some inputs second performs better. 2) It might also be possible that for some inputs, the first algorithm performs better on one machine and the second works better on other machines for s...