Lab 5 Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab 5 Assignments

Subject: OOPs with Java (22CS36)


Department: CSE
Semester: III
Course Coordinator: Dr. Vani Vs

Program 6

Write a Java program that implements a multi-thread application that has three
threads. First thread generates random integer every 1 second and if the value is even,
second thread computes the square of the number and prints. If the value is odd, the
third thread will print the value of cube of the number.

CODE :

package lab5;
public class Sqube {
public static int something=0;

public static void main(String args[]) {


Thread1 a = new Thread1();//creating new reference variables
Thread2 b = new Thread2();
Thread3 c = new Thread3();
}
}

package lab5;
import java.util.Random;
public class Thread1 implements Runnable{
Random rand=new Random();//generating random number
Thread t;
Thread1(){
t= new Thread(this,"Demo Thread");
t.start();
}
public void run() {
try {
while(true) {
Sqube.something = rand.nextInt();//using random numbers
Thread.sleep(1000);
}
}
catch(Exception e) {
System.out.println("Child thread encountered a error");
}
}
}

package lab5;
public class Thread2 implements Runnable{
Thread t;
Thread2(){
t= new Thread(this,"Demo Thread");
t.start();
}
public void run() {
try {
while(true) {
if (Sqube.something%2==0) {
System.out.println("Square:"+ Sqube.something*
Sqube.something);
}
Thread.sleep(1000);

}
}
catch(Exception e) {
System.out.println("Child thread encountered a error");
}
}
}

package lab5;
public class Thread3 implements Runnable{
Thread t;
Thread3(){
t= new Thread(this,"Demo Thread");
t.start();
}
public void run() {
try {
while(true) {
if (Sqube.something%2==1) {
System.out.println("Cube:"+ Sqube.something*
Sqube.something* Sqube.something);
}
Thread.sleep(1000);
}
}
catch(Exception e) {
System.out.println("Child thread encountered a error");
}
}
}
OUTPUT :

Program 7

Write a Java program that correctly implements the producer consumer problem using the
concept of inter-thread communication

CODE :

package lab5_1;
//A class that represents the shared buffer or queue
public class Buffer {
// A queue to store the data
private Queue <Integer> queue;
// The maximum size of the queue
private int size;
// A constructor to initialize the queue and the size
public Buffer(int size) {
this.queue = new LinkedList<>();
this.size = size;
}
// A method to add data to the queue by the producer
public synchronized void produce(int data) {
// Check if the queue is full
while (queue.size() == size) {
// If the queue is full, the producer has to wait
try {
System.out.println("The buffer is full. Producer is waiting...");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// If the queue is not full, the producer can add data
queue.add(data);
System.out.println("Producer produced: " + data);
// Notify the consumer that the queue is not empty
notify();
}
// A method to remove data from the queue by the consumer
public synchronized int consume() {
// Check if the queue is empty
while (queue.isEmpty()) {
// If the queue is empty, the consumer has to wait
try {
System.out.println("The buffer is empty. Consumer is waiting...");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// If the queue is not empty, the consumer can remove data
int data = queue.remove();
System.out.println("Consumer consumed: " + data);
// Notify the producer that the queue is not full
notify();
return data;
}
}

package lab5_1;
//A class that represents the producer thread
class Producer implements Runnable {
// A reference to the shared buffer
private Buffer buffer;
// A constructor to initialize the buffer
public Producer(Buffer buffer) {
this.buffer = buffer;
}
// A method to run the thread
public void run() {
// A loop to produce data
for (int i = 1; i <= 10; i++) {
// Produce data and add it to the buffer
buffer.produce(i);
// Sleep for some time
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

package lab5_1;
public class Consumer implements Runnable {
// A reference to the shared buffer
private Buffer buffer;
// A constructor to initialize the buffer
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
// A method to run the thread
public void run() {
// A loop to consume data
for (int i = 1; i <= 10; i++) {
// Consume data and remove it from the buffer
buffer.consume();
// Sleep for some time
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

package lab5_1;
public class Mainclass {
public static void main(String[] args) {
// Create a buffer object with size 5
Buffer buffer = new Buffer(5);
// Create a producer thread and a consumer thread
Thread producer = new Thread(new Producer(buffer));
Thread consumer = new Thread(new Consumer(buffer));
// Start the threads
producer.start();
consumer.start();
}
}
OUTPUT :

You might also like