0% found this document useful (0 votes)
3 views8 pages

Java Last 5 QuestionsSl

The document presents two Java multithreading problems: sorting a matrix row-wise using synchronized methods with both Thread class and Runnable interface, and the producer-consumer problem. The matrix sorting solution involves creating multiple threads to sort each row of a dynamically created matrix, while the producer-consumer problem demonstrates synchronization between a producer generating data and a consumer removing it from a shared buffer. Both solutions include code implementations and outputs showcasing their functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Java Last 5 QuestionsSl

The document presents two Java multithreading problems: sorting a matrix row-wise using synchronized methods with both Thread class and Runnable interface, and the producer-consumer problem. The matrix sorting solution involves creating multiple threads to sort each row of a dynamically created matrix, while the producer-consumer problem demonstrates synchronization between a producer generating data and a consumer removing it from a shared buffer. Both solutions include code implementations and outputs showcasing their functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: Harshit Kumar

Section: Cyber
Roll Number: 10
University Roll Number: 2024180
Problem Statement 30: Write a Java multithreading program which will create a synchronized
method. Method will sort a matrix row-wise. Create three multiple threads which will create
matrix dynamically and use synchronized method.
a) By using Thread class (by using synchronized method on block)
b) By using Runnable interface (by using synchronized method on block).
Solution:
import [Link];

class q30 Thread extends Thread {

private int[] row;

private final Object lock;

RowSorterThread(int[] row, Object lock) {

[Link] = row;

[Link] = lock;

public void run() {

synchronized (lock) {

[Link]("Sorting by " +
[Link]().getName());

// Bubble sort

for (int i = 0; i < [Link] - 1; i++) {

for (int j = 0; j < [Link] - i - 1; j++) {

if (row[j] > row[j + 1]) {


int temp = row[j];

row[j] = row[j + 1];

row[j + 1] = temp;

[Link]("Sorted row: ");

for (int num : row) {

[Link](num + " ");

[Link]();

public class MatrixSortWithInput {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter number of rows: ");

int rows = [Link]();

[Link]("Enter number of columns: ");

int cols = [Link]();

int[][] matrix = new int[rows][cols];


[Link]("Enter matrix elements:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

matrix[i][j] = [Link]();

Object lock = new Object();

Thread[] threads = new Thread[rows];

for (int i = 0; i < rows; i++) {

threads[i] = new RowSorterThread(matrix[i], lock);

threads[i].setName("Thread-" + (i + 1));

threads[i].start();

// Wait for all threads to finish

for (int i = 0; i < rows; i++) {

try {

threads[i].join();

} catch (InterruptedException e) {

[Link]("Thread interrupted.");

// Print final sorted matrix

[Link]("Final sorted matrix:");


for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

[Link](matrix[i][j] + " ");

[Link]();

[Link]();

Thread class output:


Sorting by Thread-1
Sorted row: 3 5 7 9
Sorting by Thread-3
Sorted row: 0 1 10 11
Sorting by Thread-2
Sorted row: 2 4 6 8

Runnable interface output:


Sorting by Thread-1
Sorted row: 8 10 12 14
Sorting by Thread-2
Sorted row: 1 3 5 7
Sorting by Thread-3
Sorted row: 2 16 18 20
Name: Harshit Kumar
Section: Cyber
Roll Number: 10
University Roll Number: 2024180

Problem Statement 31: Consumer-Producer problem:

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.

Solution:
class ProducerConsumer {
private int[] buffer;
private int count = 0;
private int capacity;

public ProducerConsumer(int capacity) {


[Link] = capacity;
buffer = new int[capacity];
}

public synchronized void produce() {


int value = 0;
while (true) {
while (count == capacity) {
try {
wait();
} catch (InterruptedException e) {
return;
}
}
buffer[count] = value;
[Link]("Producer produced: " + value);
value++;
count++;
notifyAll();
}
}

public synchronized void consume() {


while (true) {
while (count == 0) {
try {
wait();
} catch (InterruptedException e) {
return;
}
}
int value = buffer[count - 1];
[Link]("Consumer consumed: " + value);
count--;
notifyAll();
}
}
}

public class ProducerConsumerProblem {


public static void main(String[] args) {
ProducerConsumer pc = new ProducerConsumer(5);
Thread producerThread = new Thread(() -> [Link]());
Thread consumerThread = new Thread(() -> [Link]());
[Link]();
[Link]();
}
}

Output:
Consumer consumed: 276492
Consumer consumed: 276491
Consumer consumed: 276490
Producer produced: 276495
Producer produced: 276496
Producer produced: 276497
Producer produced: 276498
Producer produced: 276499
Consumer consumed: 276499
Consumer consumed: 276498
Consumer consumed: 276497
Consumer consumed: 276496
Consumer consumed: 276495
….till infinity

You might also like