Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Java Thread - Programs

The document contains 3 Java programs that use multiple threads: 1. A program that creates 2 threads to find and print even and odd numbers from 1 to 20 using synchronization and wait/notify. 2. A program that sorts an array of integers using multiple threads, each thread sorting a segment of the array, followed by a merge sort. 3. A program that performs matrix multiplication using multiple threads, each thread calculating a segment of the result matrix.

Uploaded by

Krishanu Naskar
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)
21 views

Java Thread - Programs

The document contains 3 Java programs that use multiple threads: 1. A program that creates 2 threads to find and print even and odd numbers from 1 to 20 using synchronization and wait/notify. 2. A program that sorts an array of integers using multiple threads, each thread sorting a segment of the array, followed by a merge sort. 3. A program that performs matrix multiplication using multiple threads, each thread calculating a segment of the result matrix.

Uploaded by

Krishanu Naskar
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/ 6

Write a Java program that creates two threads to find and print even and odd numbers from 1 to 20

public class Find_Even_Odd_Number {


private static final int MAX_NUMBER = 20;
private static Object lock = new Object();
private static boolean isEvenTurn = true;

public static void main(String[] args) {


Thread evenThread = new Thread(() -> {
for (int i = 2; i <= MAX_NUMBER; i += 2) {
synchronized(lock) {
while (!isEvenTurn) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Even Number from evenThread: " + i);
isEvenTurn = false;
lock.notify();
}
}
});

Thread oddThread = new Thread(() -> {


for (int i = 1; i <= MAX_NUMBER; i += 2) {
synchronized(lock) {
while (isEvenTurn) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Odd Number from oddThread: " + i);
isEvenTurn = true;
lock.notify();
}
}
});

evenThread.start();
oddThread.start();
}
}
Write a Java program that sorts an array of integers using multiple threads

import java.util.Arrays;

public class ParallelSort {


private static final int ARRAY_SIZE = 400;
private static final int NUM_THREADS = 4;

public static void main(String[] args) {


int[] array = createArray();
System.out.println("Before sorting: " + Arrays.toString(array));

Thread[] threads = new Thread[NUM_THREADS];


int segmentSize = ARRAY_SIZE / NUM_THREADS;

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


int startIndex = i * segmentSize;
int endIndex = (i == NUM_THREADS - 1) ? ARRAY_SIZE - 1 : (startIndex + segmentSize - 1);
threads[i] = new Thread(new SortTask(array, startIndex, endIndex));
threads[i].start();
}

for (Thread thread: threads) {


try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

mergeSort(array, 0, ARRAY_SIZE - 1);

System.out.println("After sorting: " + Arrays.toString(array));


}

private static int[] createArray() {


int[] array = new int[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = (int)(Math.random() * 400); // Generate random numbers between 0 and 400
}
return array;
}

private static void mergeSort(int[] array, int left, int right) {


if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
}

private static void merge(int[] array, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left, j = mid + 1, k = 0;

while (i <= mid && j <= right) {


if (array[i] <= array[j]) {
temp[k++] = array[i++];
} else {
temp[k++] = array[j++];
}
}

while (i <= mid) {


temp[k++] = array[i++];
}

while (j <= right) {


temp[k++] = array[j++];
}

System.arraycopy(temp, 0, array, left, temp.length);


}

static class SortTask implements Runnable {


private int[] array;
private int startIndex;
private int endIndex;

public SortTask(int[] array, int startIndex, int endIndex) {


this.array = array;
this.startIndex = startIndex;
this.endIndex = endIndex;
}

@Override
public void run() {
Arrays.sort(array, startIndex, endIndex + 1);
}
}
}
Write a Java program that performs matrix multiplication using multiple threads.

public class MatrixMultiplication {


private static final int MATRIX_SIZE = 3;
private static final int NUM_THREADS = 2;

public static void main(String[] args) {


int[][] matrix1 = {
{
1,
2,
3
},
{
4,
5,
6
},
{
7,
8,
9
}
};

int[][] matrix2 = {
{
9,
8,
7
},
{
6,
5,
4
},
{
3,
2,
1
}
};

int[][] result = new int[MATRIX_SIZE][MATRIX_SIZE];

Thread[] threads = new Thread[NUM_THREADS];


int segmentSize = MATRIX_SIZE / NUM_THREADS;
for (int i = 0; i < NUM_THREADS; i++) {
int startIndex = i * segmentSize;
int endIndex = (i == NUM_THREADS - 1) ? MATRIX_SIZE - 1 : (startIndex + segmentSize - 1);
threads[i] = new Thread(new MultiplicationTask(matrix1, matrix2, result, startIndex, endIndex));
threads[i].start();
}

for (Thread thread: threads) {


try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

// Print the result matrix


System.out.println("Result:");
for (int[] row: result) {
for (int element: row) {
System.out.print(element + " ");
}
System.out.println();
}
}

static class MultiplicationTask implements Runnable {


private int[][] matrix1;
private int[][] matrix2;
private int[][] result;
private int startIndex;
private int endIndex;

public MultiplicationTask(int[][] matrix1, int[][] matrix2, int[][] result, int startIndex, int endIndex) {
this.matrix1 = matrix1;
this.matrix2 = matrix2;
this.result = result;
this.startIndex = startIndex;
this.endIndex = endIndex;
}

@Override
public void run() {
int cols = matrix2[0].length;

for (int i = startIndex; i <= endIndex; i++) {


for (int j = 0; j < cols; j++) {
for (int k = 0; k < MATRIX_SIZE; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
}
}

You might also like