Java Lab8 20BCE1794 Shreya
Java Lab8 20BCE1794 Shreya
}
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");
}
}
}
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();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
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:
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){}
else{
System.out.println("Writer "+num+" is writing");
writing=writing+1;
try{
Thread.sleep(5000);
}catch(Exception ignore){}
else{
System.out.println("Reader "+num+" is reading");
try{
Thread.sleep(5000);
}catch(Exception ignore){}
System.out.println("Reader "+num+" finished reading");
}
}
}
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:
class Account{
int amount;
public Account(int balance){
amount=balance;
}
this.amount= this.amount-amount;
System.out.println("Amount has been withdrawn");
}
}
OUTPUT: