Thread Synchronization

Synchronization, Producer and Consumer Problem, wait(), notify() methods of the Thread

class Controller
{
  private int counter;

  public void set(int i)
  {
      counter=i;
  }

  public int get()
  {
    return counter;
  }
}

class Producer extends Thread
{
  private Controller obj;

 public Producer(Controller c)
 {
  Obj=c;
 }

public void run()
{
 for(int i=0;I<5;i++ )
 {
   obj.set(i);
   System.out.println("put:"+j);
 }
}
}

class Consumer extends Thread
{
  private  Controller obj;

  public Consumer(Controller c)
  {
    obj=c;
  }

  public void run()
  {
    for(int  i=0;i<5;i++)
    {
        System.out.println("get:"+obj.get());
     }
  }
}

class TestSynchronization
{
 public  static void main(String args[])
 {
   Controller ctrl=new Controller;
   Producer thread1=new producer(ctrl);
   Consumer thread2=new Consumer(ctrl);
   thread1.start();
   thread2.start();
  }
}

O/P:-

put:0
get:0
get:0
get:0
get:0
get:0
put:1
put:2
put:3
put:4

Explanation:

In above program the producer creates value faster or slower than consumer that means there is no synchronization & this problem is known as producer_consumer problem.

Solution Of Producer & Customer Problem:-

Synchronized:

The synchronized block or synchronized method, allow only one thread at a time to execute.

Wait():

This method will cause the current thread to go into wait state until & unless notify() or notifyAll() is not called.

notifyAll():

It will give signal to all the threads which are in state to continue.

class Controller
{
  private int counter;
  private Boolean available=false;
  public synchronized int get()
  {
      while(available==false)
     {
      try
        {
          wait();
        }
        catch(Exception e)
        {
        }
     }
     notifyAll();
    available=false;
    return counter;
  }

 public synchronized int set(int value)
 {
   while(available==true)
   {
     try
     {
        wait();
     }
     catch(Exception e)
     {
     }
     counter=value;
     available=false;
     notifyAll();
    }
  }
}

class Producer extends Thread
{
  private Controller obj;
  public Producer(Controller c)
  {
   Obj=c;
  }

  public void run()
  {
   for(int i=0;I<5;i++ )
   {
     obj.set(i);
     System.out.println("put:"+j);
   }
 }
}

class Consumer extends Thread
{
 private  Controller obj;
 public Consumer(Controller c)
 {
  obj=c;
 }
 public void run()
 {
  for(int  i=0;i<5;i++)
  {
     System.out.println("get:"+obj.get());
  }
 }
}

class TestSynchronization
 {
  public  static void main(String args[])
  {
     Controller ctrl=new Controller;
     Producer thread1=new producer(ctrl);
     Consumer thread2=new Consumer(ctrl);
     thread1.start();
     thread2.start();
   }
}

O/P:
put:0
get:0
put:1
get:1
put:2
get:2
put:3
get:3
put:4
get:4

Posted

in

by

Tags:


Related Posts

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading