Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label THIRD WEEK LAB PROGRAM. Show all posts
Showing posts with label THIRD WEEK LAB PROGRAM. Show all posts

Wednesday, 6 February 2019

THIRD WEEK TERM WORK Program 4

4) Write a Java program that implements bounded buffer problem (consider a circular queue
and single producer and consumer threads).


import java.util.Scanner;

class Qb {
int n;
int a[] = new int[5];
boolean valueSet = false;
private Scanner s;
int front = -1;
int rear = -1;
int result;

synchronized int get() {
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println();

for (int i = 0; i <= 4; i++) {
result = deleteQ();
System.out.println("Consumed#" + (i + 1) + ": " + result);
valueSet = false;
}
notify();
return n;
}

synchronized void put(int n) {
while (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
for (int i = 0; i <= 4; i++) {
this.n = n;
valueSet = true;
System.out.println("enter the value u want to push in ");
s = new Scanner(System.in);
result = s.nextInt();
System.out.println("Producer#" + (i + 1) + ": " + result);
insertQ(result);
}
notify();
}

void insertQ(int m) {
if ((front == 0 && rear == 4) || (front == rear + 1)) {
System.out.println("Production Queue is Full");
} else if (rear == -1) {
rear++;
front++;
a[rear] = m;
} else {
rear++;
a[rear] = m;
}

}

int deleteQ() {
int m;
if (front == -1 && rear == -1) {
System.out.println("NO More Produced Queue is empty");
return -1;
} else if (front == rear) {
if (front == 0) {
rear = -1;
front = -1;
System.out.println("NO More Produced Queue is empty");
return -1;

}
m = front;
rear = -1;
front = -1;
return a[m];
} else if (front == 5) {
front = 0;
return a[front];

} else {
m = a[front];
++front;
return m;

}
}
}

class Producers implements Runnable {
Qb q;

Producers(Qb q) {
this.q = q;
new Thread(this, "Producer").start();
}

public void run() {
int i = 0;
while (i == 0) {
q.put(i);
}
}
}

class Consumers implements Runnable {
Qb q;

Consumers(Qb q) {
this.q = q;
new Thread(this, "Consumer").start();
}

public void run() {
int i = 0;
while (i == 0) {
q.get();
}
}
}

class BoundBuffer {
public static void main(String args[]) {
Qb q = new Qb();
new Producers(q);
new Consumers(q);
System.out.println("Press Control-C to stop.");
}
}


THIRD WEEK TERM WORK Program 5

5) Write a Java program that simulates deadlock condition using threads.

Program:

public class DeadLockThread {
   public static Object L1 = new Object();
   public static Object L2 = new Object();
   
   public static void main(String args[]) {
      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }
   
   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (L1) {
            System.out.println("Thread 1: Holding lock 1...");
            
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");

            

            synchronized (L2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (L2) {
            System.out.println("Thread 2: Holding lock 2...");
            
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 1...");
            
            synchronized (L1) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   } 
}

THIRD WEEK TERM WORK Program 3

3) Write a Java Program that simulates Client-Server Interaction using threads.
Methodology: Two threads should be created; one thread should act as Server, and the other
one should act as Client. The Server thread should accept a String request from Client thread,
parse that request and print the length of the string. It should also send back the
acknowledgement message “Message Received” to Client Thread. The Client thread should
terminate only when it receives acknowledgement from Server thread.


import java.util.Scanner;

class Qa {
int n;
String msg="a";
boolean valueSet = false;
synchronized String get() {
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
valueSet = false;
notify();
return msg;
}

synchronized void put(String msg) {
while (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.msg=msg;
valueSet =true;
notify();
}
}

class Server implements Runnable {
Qa q;
String msg="a";
Server(Qa q) {
this.q = q;
new Thread(this, "Server").start();
}

public void run() {
msg=q.get();
System.out.println("Message:"+msg);
System.out.println("Message Length "+msg.length());
q.put("Message Received");
}
}


class Client implements Runnable {
Qa q;
Scanner input=new Scanner(System.in);
String msg;
Client(Qa q) {
this.q = q;
new Thread(this, "Client").start();
}

public void run() {
System.out.println("Enter the String");
msg=input.nextLine();
q.put(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!(msg=q.get()).equals("Message Received"));
System.out.println("Message Received");
}
}

class clientserverthread {
public static void main(String args[]) {
Qa q = new Qa();
new Client(q);
new Server(q);
}

}

THIRD WEEK TERM WORK Program 2

2) Write a Java program that implements producer consumer problem (consider a single
resource slot and single producer and consumer threads).


import java.util.Scanner;

//A correct implementation of a producer and consumer.
class Q {
int n;
int a;
boolean valueSet = false;
private Scanner s;

synchronized int get() {
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Consumed: " + a);
valueSet = false;
notify();
return n;
}

synchronized void put(int n) {
while (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("enter the value u want to push in ");
s = new Scanner(System.in);
a = s.nextInt();
System.out.println("Produced#: " + a);
notify();
}
}

class Producer implements Runnable {
Q q;

Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}

public void run() {
int i = 1;
q.put(i);
}
}

class Consumer implements Runnable {
Q q;

Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}

public void run() {
q.get();
}
}

class producerconsumer {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-Z to stop.");
}
}

Tuesday, 5 February 2019

THIRD WEEK TERM WORK Program 1 (1b)

Question:

Write a Java program that uses threads to compute multiplication of two matrices. The program should perform multiplication of matrices of arbitrary order. Also, proper error
handling mechanism should be used.
Methodology: Matrix multiplication is implemented using the formula: Cij = Aik * Bkj.
This formula is used for generating each element of the final matrix. The multiplication must be performed by separate threads.

 Ex: If the order of the final matrix is 3X3, then a total of 9 threads should be created; each thread computing individual elements of the final matrix.

Solution:


import java.util.*;

class MatrixMultiple extends Thread {
Mutiple obj;

public MatrixMultiple(Mutiple obj) {
// TODO Auto-generated constructor stub
this.obj = obj;
}

void run(int i, int j, int m) {
obj.multiple(i, j, m);
}
}

public class firstProgram {
static int[][] mat1;
static int[][] mat2;
static int[][] matResult;

public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
System.out.println("Enter first Matrix index nXm");
int n = input.nextInt();
int m = input.nextInt();
System.out.println("Enter second Matrix index pXq");
int p = input.nextInt();
int q = input.nextInt();
if (n!= q) {
System.out.println("m is not p matrix Multiplication is not possible");
System.exit(-1);
}
mat1 = new int[n][m];
mat2 = new int[p][q];
matResult = new int[n][q];
System.out.println("Enter the values for first matrix");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat1[i][j] = input.nextInt();
}
}
System.out.println("Enter the values for Second matrix");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
mat2[i][j] = input.nextInt();
}
}
Mutiple mul = new Mutiple();
int nt = n * q, k = 0;

MatrixMultiple obj[] = new MatrixMultiple[nt];
try {
for (int i = 0; i < n; i++) {
for (int j = 0; j < q; j++, k++) {
obj[k] = new MatrixMultiple(mul);
obj[k].start();
obj[k].run(i, j, m);
obj[k].join();

}

}
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("RESULT MATRIX");
for (int i = 0; i < n; i++) {
for (int j = 0; j < q; j++, k++) {
System.out.print(matResult[i][j] + " ");
}
System.out.println();
}
}
}

class Mutiple extends firstProgram {
public void multiple(int i, int j, int m) {
int sum = 0;
for (int y = 0; y < m; y++) {
sum = sum + mat1[i][y] * mat2[y][j];
}
matResult[i][j] = sum;
}
}