Skip to main content

Posts

java.util.concurrent.locks package program

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();  ...
Recent posts

Deep Analysis of Algorithm(part 1)

 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...

Data structure and algorithm

Algorithm An algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. A n algorithm can be implemented in more than one programming language. Characteristics of Algorithm Unambiguous(spasht in Hindi) - Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. Input- An algorithm should have 0 or more well-defined inputs. Output-  An algorithm should have 1 or more well-defined outputs and should match the desired output. Finiteness  −  Algorithms must terminate after a finite number of steps. Feasibility  −  Should be feasible with the available resources. Independent  −  An algorithm should have step-by-step directions, which should be independent of any programming code. Algorithm Analysis:- A Priori Analysis-  This is a theoretical analysis of an algorithm. A Posterior Analysis-  This is an empirical analysis of ...

Multi-threading enhancement

java.util.concurrent.lock package{1.5 version} Problem with tradition synchronized  keyword: We are not having any flexibility to try for a lock without waiting. There is no way to specify the maximum waiting   time for a thread to get the lock whereas in tradition synchronized thread will wait until getting the lock which may cause performance problems or deadlock. In  tradition synchronized,  if a thread releases the lock then which waiting thread will get that lock we are not having control over this To overcome this problem sun organization introduces java.util.lock package in JDK 1.5v. It also provides several enhancement to the programmers.  Lock Interface: Lock object is similar to the implicit lock to execute synchronized methods or blocks. Lock interface provides more extensive operation than traditional lock(synchronized). Important methods of Lock interface: void lock() - we can use this method to acquire a lock.       Different Cases:...

Multi-threading enhancement

ThreadGroup: Based on the  functionality we can group thread into a single unit which is nothing but a thread group i.e a thread group contains a group of threads. In addition to that, it also contains a sub-thread group The main advantage of maintaining threads in the form of the thread group is we can perform common operations very easily. Note:-1. Every thread in java belong to some group, main thread belong to the main group            2. Every thread group in java is the child group of the system group either directly or indirectly, hence a system group act as root/parent for all thread groups. System group contains several system levels thread:  1. finalizer  2. Reference handler  3. Attach Listener  4. Signal Dispatcher, etc. Program to illustrate that every thread in java belongs to some group and directly or indirectly child of system group:  import java.lang.*; class ThreadGroupDemo1{    public static void m...

Daemon Thread

Daemon Thread:   1 . The thread which are executing in background is called Daemon thread .         For example:-Garbage Collector,Signal Dispatcher,Attach Listener etc.       2.The main objective of daemon Thread is to provide support to Non-daemon thread .       3. Usually daemon thread having low priority but based on our requirement daemon thread can also run with high priority by JVM .      4. By default main thread is always non-daemon and for all remaining thread daemon nature is inherited i.e if the parent thread is daemon then automatically child thread is also daemon and vice -versa.  5.JVM terminate all daemon thread when all user thread finish their execution.     Note:-Changing nature of thread to daemon is only possible before starting a thread .After starting a thread if we are trying to change thread to daemon thread then it will throw an exception illegalThreadStateException on run...

DeadLock Program

Program On DeadLock:     import java.util.*;     class A{        public synchronized void d1(B b)          {             System.out.println("Thread start executing d1() method");             try{               Thread.sleep(5000);               }catch(Exception e){}            System.out.println("Thread try to invoke B's last()");             b.last();        }      public synchronized void last()      {          System.out.println("last() method of A's class");      }    }     class B{        public synchronized void d2(A a)          {             System....