Thursday, January 9, 2020

Exercise - 11 (Threads continuity)

Exercise - 11 (Threads continuity)

a).Write a JAVA program Producer Consumer Problem

import java.util.LinkedList; 
public class Threadexample 

public static void main(String[] args) throws InterruptedException 

final PC pc = new PC(); 
Thread t1 = new Thread(new Runnable() 

@Override
public void run() 

try

pc.produce(); 

catch(InterruptedException e) 

e.printStackTrace(); 


}); 
Thread t2 = new Thread(new Runnable() 

@Override
public void run() 

try

pc.consume(); 

catch(InterruptedException e) 

e.printStackTrace(); 


}); 
t1.start(); 
t2.start(); 
t1.join(); 
t2.join(); 

public static class PC 

LinkedList<Integer> list = new LinkedList<>(); 
int capacity = 4; 
public void produce() throws InterruptedException 

int value = 0; 
while (true) 

synchronized (this) 

while (list.size()==capacity) 
wait(); 
System.out.println("Producer produced-"+ value); 
list.add(value++); 
notify(); 
Thread.sleep(1000); 



public void consume() throws InterruptedException 

while (true) 

synchronized (this) 

while (list.size()==0) 
wait(); 
int val = list.removeFirst(); 
System.out.println("Consumer consumed-"+ val); 
notify(); 
Thread.sleep(1000); 





}

b).Write a case study on thread Synchronization after solving the above producer consumer
problem

In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue.
  • The producer’s job is to generate data, put it into the buffer, and start again.
  • At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.
Problem
To make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.
Solution
The producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
Implementation of Producer Consumer Class
  • LinkedList list – to store list of jobs in queue.
  • A Variable Capacity – to check for if the list is full or not
  • A mechanism to control the insertion and extraction from this list so that we do not insert into list if it is full or remove from it if it is empty.
In PC class (A class that has both produce and consume methods), a linked list of jobs and a capacity of the list is added to check that producer does not produce if the list is full.

In Producer class, the value is initialized as 0.Also, we have an infinite outer loop to insert values in the list. Inside this loop, we have a synchronized block so that only a producer or a consumer thread runs at a time.An inner loop is there before adding the jobs to list that checks if the job list is full, the producer thread gives up the intrinsic lock on PC and goes on the waiting state.If the list is empty, the control passes to below the loop and it adds a value in the list.

In the Consumer class, we again have an infinite loop to extract a value from the list.Inside, we also have an inner loop which checks if the list is empty.If it is empty then we make the consumer thread give up the lock on PC and passes the control to producer thread for producing more jobs. If the list is not empty, we go round the loop and removes an item from the list.


In both the methods, we use notify at the end of all statements. The reason is simple, once you have something in list, you can have the consumer thread consume it, or if you have consumed something, you can have the producer produce something. sleep() at the end of both methods just make the output of program run in step wise manner and not display everything all at once so that you can see what actually is happening in the program.


Wednesday, January 1, 2020

Exercise – 10 (Threads)

Exercise – 10 (Threads)

a). Write a JAVA program that creates threads by extending Thread class . First thread display
"Good Morning " every 1 sec, the second thread displays "Hello"every 2 seconds and the
third display Welcome" every 3 seconds , (Repeat the same by implementing Runnable)

import java.io.*;
class A extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(1000);
System.out.println("good morning");
}
}
catch(Exception e)
{ }
}
}
class B extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(2000);
System.out.println("hello");
}
}
catch(Exception e)
{ }
}
}
class C extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(3000);
System.out.println("welcome");
}
}
catch(Exception e)
{ }
}
}
class Threadmsgs
{
    public static void main(String args[])
    {
        A t1=new A();
        B t2=new B();
        C t3=new C();
        t1.start();
        t2.start();
        t3.start();
    }
}

b). Write a program illustrating isAlive and join ()

public class AliveJoin extends Thread 
public void run() 
System.out.println("ABDUL KALAM "); 
try 
Thread.sleep(300); 
catch (InterruptedException ie) 
{ } 
System.out.println("SUBHASH CHANDRABOSH "); 
public static void main(String[] args) 
AliveJoin c1 = new AliveJoin(); 
AliveJoin c2 = new AliveJoin(); 
c1.start(); 
c2.start();
System.out.println(c1.isAlive());
System.out.println(c2.isAlive());
try 
c1.join(); // Waiting for c1 to finish 
catch (InterruptedException ie) 
{ } 
}

c). Write a Program illustrating Daemon Threads.

import java.io.*;
public class DaemonThread extends Thread 
public DaemonThread(String name)
super(name); 
public void run() 
{  
if(Thread.currentThread().isDaemon()) 
{  
            System.out.println(getName() + " is Daemon thread");  
}  
else
{  
System.out.println(getName() + " is User thread");  
}  
}  
public static void main(String[] args) 
{  

        DaemonThread t1 = new DaemonThread("t1"); 
        DaemonThread t2 = new DaemonThread("t2"); 
        DaemonThread t3 = new DaemonThread("t3");
        t1.setDaemon(true);                
        t1.start();  
        t2.start(); 
        t3.setDaemon(true);  
        t3.start();         
    }  
}

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS

SIR C R REDDY COLLEGE OF ENGINEERING, ELURU DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS ...