0% found this document useful (0 votes)
45 views12 pages

Java Lab8 20BCE1794 Shreya

This document contains code for 5 Java programming exercises involving thread synchronization, inter-process communication, and exceptions: 1. The first exercise creates 3 threads - one to generate perfect squares, one for the 9x table, and one to print a sequence. Synchronized and join methods are used. 2. The second implements a producer-consumer problem with one producer thread and one consumer thread sharing a fixed-size buffer. 3. The third simulates the reader-writer problem, allowing multiple reader threads but only one writing thread at a time. 4. The fourth demonstrates different types of runtime exceptions - arithmetic, null pointer, out of bounds, number format, and file not found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views12 pages

Java Lab8 20BCE1794 Shreya

This document contains code for 5 Java programming exercises involving thread synchronization, inter-process communication, and exceptions: 1. The first exercise creates 3 threads - one to generate perfect squares, one for the 9x table, and one to print a sequence. Synchronized and join methods are used. 2. The second implements a producer-consumer problem with one producer thread and one consumer thread sharing a fixed-size buffer. 3. The third simulates the reader-writer problem, allowing multiple reader threads but only one writing thread at a time. 4. The fourth demonstrates different types of runtime exceptions - arithmetic, null pointer, out of bounds, number format, and file not found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CSE1007 JAVA LAB CLASS

EXERCISE 8 - THREAD SYNCHRONIZATION AND


INTERPROCESS COMMUNICATION
Shreya Sivakumar 20BCE1794
L39+40

1. Devise a Java program with three thread synchronization


(thread-1 to generate perfect square numbers,
thread-2 to generate 9 th multiplication table and
thread-3 to print the sequence 3,6,18,72…..)
Use both synchronized / joint () methods
CODE:
import java.util.*;
class thread_1 extends Thread{
synchronized public void isPrime(int n){
int i,m=0, flag=0;
m=n/2;
if(n==0 ||n ==1){

}
else{

for(i=2;i<m;i++){
if(n%i==0){
flag=1;
break;
}
}
if(flag==0){
System.out.println("Thread 1: "+n+" is a prime number");
}
}
}

synchronized public void run(){


int n;
for(n=1;n<100;n++){
isPrime(n);
}
}
}

class thread_2 extends Thread{


synchronized public void run(){
int n;
for(n=1;n<20;n++){
System.out.println("Thread 2: Multiples of 11 x "+n+" ="+
(n*11));
}
}
}

class thread_3 extends Thread{


synchronized public void run(){
int n, seq=3;
for(n=1;n<10;n++){
seq=seq*n;
System.out.println("Thread 3: Sequence "+seq);
}
}
}

public class L8_01{


public static void main(String args[]){
thread_1 t1= new thread_1();
thread_2 t2= new thread_2();
thread_3 t3= new thread_3();

t1.start();
try{
t1.join();
}
catch(Exception e) {
System.out.println(e);
}

t2.start();
try{
t2.join();
}
catch(Exception e){
System.out.println(e);
}

t3.start();
try{
t3.join();
}
catch(Exception e){
System.out.println(e);
}
}
}
OUTPUT:
2. There is one Producer in the producer-consumer problem, Producer
is producing some items, whereas there is one Consumer that is
consuming the items produced by the Producer. The same memory
buffer is shared by both producers and consumers which is of fixed-
size. Devise a Java program to producer-consumer problem of
operating system using interthread communication function call.
CODE:
import java.util.LinkedList;
public class L8_02 {
public static void main(String[] args) throws InterruptedException{
final Prod_Cons proco = new Prod_Cons();
Thread t1 = new Thread(new Runnable() {
@Override
public void run(){
try {
proco.produce();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});

Thread t2 = new Thread(new Runnable() {


@Override
public void run(){
try {
proco.consume();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});

t1.start();
t2.start();

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

public static class Prod_Cons {


LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
public void produce() throws InterruptedException
{
int value = 0;
while (true) {
synchronized (this){
while (list.size() == capacity)
wait();

System.out.println("Producer-"+ 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-"+ val);

notify();
Thread.sleep(1000);
}
}
}
}
}

OUTPUT:

3. There are many competing threads wishing to read and write. It is


acceptable to have multiple threads reading at the same time, but if
one thread is writing then no other process may either read or write.
Devise a Java program to simulate reader writer problem of operating
system using interthread communication function call. Incorporate
wait(), notify() and notifyall() of thread class methods and introduce
sufficient sleep() to simulate the reading and writing.
CODE:
import java.util.*;
public class L8_03{
public static void main(Sting args[]){
Readwrite rw=new Readwrite();
Reader r1= new Reader(rw,1);
Reader r2= new Reader(rw,2);
Reader r3= new Reader(rw,3);
Reader r4= new Reader(rw,4);

Writer w1= new Writer(rw,1);


Writer w2= new Writer(rw,2);
Writer w3= new Writer(rw,3);

w1.start();
try{
Thread.sleep(1000);
}catch(Exception ignore){}

r1.start();
try{
Thread.sleep(1000);
}catch(Exception ignore){}

r2.start();
try{
Thread.sleep(1000);
}catch(Exception ignore){}

r3.start();
try{
Thread.sleep(1000);
}catch(Exception ignore){}

w2.start();
try{
Thread.sleep(1000);
}catch(Exception ignore){}

w3.start();
try{
Thread.sleep(1000);
}catch(Exception ignore){}

r4.start();
}
}

class Readwrite{
private int writing=0;
public void write(int num){
if(writing>0){
System.out.println("Writer "+num+" is waiting as writer is
writing");
while (writing>0){
try{
wait();
} catch(Exception ignore){}
}
writing=writing+1;
System.out.println("Writer "+num+" is writing");

try{
Thread.sleep(5000);
}catch(Exception ignore){}

System.out.println("Writer "+num+" finished writing");


writing=writing-1;
notifyAll();
}

else{
System.out.println("Writer "+num+" is writing");
writing=writing+1;
try{
Thread.sleep(5000);
}catch(Exception ignore){}

System.out.println("Writer "+num+" finished writing");


writing=writing-1;
notifyAll();
}
}

public void read(int num){


if(writing>0){
System.out.println("Reader "+num+" is waiting as writer is
writing");
while(writing>0){
try{
wait();
} catch(Exception ignore){}
}
System.out.println("Reader "+num+" is reading");
try{
Thread.sleep(5000);
}catch(Exception ignore){}
System.out.println("Reader "+num+" finished reading");
}

else{
System.out.println("Reader "+num+" is reading");
try{
Thread.sleep(5000);
}catch(Exception ignore){}
System.out.println("Reader "+num+" finished reading");
}
}
}

class Reader extends Thread{


private Readwrite readwrite;
private int number;

public Reader(Readwrite rw, int num){


readwrite=rw;
this.number=num;

public void run(){


readwrite.read(number);
}
}

class Writer extends Thread{


private Readwrite readwrite;
private int number;

public Writer(Readwrite rw, int num){


readwrite= rw;
this.number=num;
}

public void run(){


readwrite.read(number);
}
}

OUTPUT:
4. Devise a Java program to illustrate any three types of run time
exceptions
CODE:
import java.io.*;
public class L8_04{
public static void main(String args[]){
try{
int a=30;
int b=0;
int c=a/b;
System.out.println("Result= "+c);
}
catch(ArithmeticException e){
System.out.println("Arithmetic Exception, Can't divide a number
by 0");
}

try{
String a=null;
System.out.println(a.charAt(0));
}
catch(NullPointerException e){
System.out.println("Null Pointer Exception, no character at index
0");
}

try{
String a="This is VIT";
char c=a.charAt(44);
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e){
System.out.println("String Index Out Of Bounds Exception, no
character at index 44");
}

try{
int num=Integer.parseInt("Shreya");
System.out.println(num);
}
catch(NumberFormatException e){
System.out.println("Number Format Exception, you entered a
sting");
}

try{
File file=new File("/filer2");
FileReader fr= new FileReader(file);
}
catch(FileNotFoundException e){
System.out.println("File Not Found Exception, invalid file name");
}
}
}

OUTPUT:

5. Devise a program to withdraw the amount from the bank account.


While withdraw, the program should check the amount in the account.
If the amount in the account is > than the amount entered then raise
the exception “Amount withdraw failure” else withdraw the amount.
CODE:
import java.io.*;
public class L8_05{
public static void main(String args[]){
Account a=new Account(50000);
User t1= new User(a);
t1.start();
Account b=new Account(30000);
User t2=new User(b);
t2.start();
}
}

class Account{
int amount;
public Account(int balance){
amount=balance;
}

synchronized void withdraw(int amount)throws IOException{


if(this.amount<amount){
throw new IOException("Failure to withdraw ammount");
}

this.amount= this.amount-amount;
System.out.println("Amount has been withdrawn");
}
}

class User extends Thread{


Account ob1;
User(Account a){
this.ob1=a;
}

public void run(){


try{
this.ob1.withdraw(5000);
}
catch(Exception e){
System.out.println(e);
}
}
}

OUTPUT:

You might also like