r19 JNTUK JAVA LAB ; Exercise-11

Exercise-11:

 a)Producer-Consumer problem

AIM: Write a JAVA program Producer Consumer Problem

SOURCE-CODE:

class A

{

int n;

boolean b=false;

synchronized int get()

{

if(!b)

try

{

wait();

}

catch(Exception e)

{

System.out.println(e);

}

System.out.println("Got:"+n);

b=false;

notify();

return n;

}

synchronized void put(int n)

{

if(b)

try

{

wait();

}

catch(Exception e)

{

System.out.println(e);

}

this.n=n;

b=true;

System.out.println("Put:"+n);

notify();

}

}

class producer implements Runnable

{

A a1;

Thread t1;

producer(A a1)

{

this.a1=a1;

t1=new Thread(this);

t1.start();

}

public void run()

{

for(int i=1;i<=10;i++)

{

a1.put(i);

}

}

}

class consumer implements Runnable

{

A a1;

Thread t1;

consumer(A a1)

{

this.a1=a1;

t1=new Thread(this);

t1.start();

}

public void run()

{

for(int j=1;j<=10;j++)

{

a1.get();

}

}

}

class interdemo

{

public static void main(String args[])

{

A a1=new A();

producer p1=new producer(a1);

consumer c1=new consumer(a1);

}

}


OUT-PUT:

Put:1

Got:1

Put:2

Got:2

Put:3

Got:3

Put:4

Got:4

Put:5

Got:5

Put:6

Got:6

Put:7

Got:7

Put:8

Got:8

Put:9

Got:9

Put:10

Got:10


b)Case study on thread synchronization

AIM: To write a case study on thread Synchronization after solving the above producer

consumer problem

A case study on thread synchronization after solving producer consumer problem:

We can use wait, notify and notifyAll methods to communicate between threads in Java.

For example, if we have two threads running in your SOURCE-CODE e.g.Producer and

Consumer then producer thread can communicate to the consumer that it can start

consuming now because there are items to consume in the queue.

Similarly, a consumer thread can tell the producer that it can also start putting items now

because there is some space in the queue, which is created as a result of

consumption.

A thread can use wait() method to pause and do nothing depending upon some condition.

For example, in the producer-consumer problem, producer thread should wait if the queue

is full and consumer thread should wait if the queue is empty.

If some thread is waiting for some condition to become true, we can use notify and

notifyAll methods to inform them that condition is now changed and they can wake

up.

Both notify() and notifyAll() method sends a notification but notify sends the notification

to only one of the waiting thread, no guarantee which thread will receive notification

and notifyAll() sends the notification to all threads.

Things to remember:

1. We can use wait() and notify() method to implement inter-thread communication in Java.

Not just one or two threads but multiple threads can communicate to each other by

using these methods.

2. Always call wait(), notify() and notifyAll() methods from synchronized method or

synchronized block otherwise JVM will throw IllegalMonitorStateException.

3. Always call wait and notify method from a loop and never from if() block, because loop

test waiting condition before and after sleeping and handles notification even if

waiting for the condition is not changed.

4. Always call wait in shared object e.g. shared queue in this example.

5. Prefer notifyAll() over notify() method due to reasons given in this article

Comments