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

0373 OS ThreadLab

The document contains three Java code examples demonstrating multithreading. The first example calculates the average, maximum, and minimum of an integer array using separate threads. The second example searches for a keyword in an array of strings using multiple threads, while the third example implements a producer-consumer model using a blocking queue for event notifications.

Uploaded by

anasyousaf440
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)
2 views8 pages

0373 OS ThreadLab

The document contains three Java code examples demonstrating multithreading. The first example calculates the average, maximum, and minimum of an integer array using separate threads. The second example searches for a keyword in an array of strings using multiple threads, while the third example implements a producer-consumer model using a blocking queue for event notifications.

Uploaded by

anasyousaf440
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

Task1:

CODE:
class AvgThread extends Thread {

int[] arr;

AvgThread(int[] arr) { [Link] = arr; }

public void run() {

double sum = 0;

for (int n : arr) sum += n;

[Link]("Average: " + (sum / [Link]));

class MaxThread extends Thread {

int[] arr;

MaxThread(int[] arr) { [Link] = arr; }

public void run() {

int max = arr[0];

for (int n : arr) if (n > max) max = n;

[Link]("Max: " + max);

class MinThread extends Thread {

int[] arr;
MinThread(int[] arr) { [Link] = arr; }

public void run() {

int min = arr[0];

for (int n : arr) if (n < min) min = n;

[Link]("Min: " + min);

public class Main {

public static void main(String[] args) throws InterruptedException {

int[] arr = new int[[Link]];

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

arr[i] = [Link](args[i]);

Thread t1 = new AvgThread(arr);

Thread t2 = new MaxThread(arr);

Thread t3 = new MinThread(arr);

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();
[Link]("Statistics complete.");

OUTPUT:

Task2:

CODE:

class SearchThread extends Thread { String[] arr; String keyword; int start, end; Thread[]
threads;

SearchThread(String[] arr, String keyword, int start, int end, Thread[] threads) {
[Link] = arr;
[Link] = keyword;
[Link] = start;
[Link] = end;
[Link] = threads;
}
public void run() {
for (int i = start; i < end; i++) {
if ([Link]().isInterrupted()) return;

if (arr[i].equals(keyword)) {
[Link]("Found at index: " + i);

for (Thread t : threads) {


if (t != this) [Link]();
}
return;
}
}
}

public class Main { public static void main(String[] args) { String[] data = {"apple", "banana",
"cat", "dog", "elephant", "fish", "goat", "hat"}; String keyword = "dog";

Thread[] threads = new Thread[4];


int n = [Link] / 4;

threads[0] = new SearchThread(data, keyword, 0, n, threads);


threads[1] = new SearchThread(data, keyword, n, 2*n, threads);
threads[2] = new SearchThread(data, keyword, 2*n, 3*n, threads);
threads[3] = new SearchThread(data, keyword, 3*n, [Link], threads);

for (Thread t : threads) [Link]();


}

OUTPUT:
TASK-3:

CODE:
import [Link];

class Producer extends Thread {

LinkedBlockingQueue<String> queue;

String[] events;

Producer(LinkedBlockingQueue<String> queue, String[] events) {

[Link] = queue;

[Link] = events;

public void run() {

try {

for (String event : events) {

[Link](event);

[Link](500);
}

} catch (InterruptedException e) {

[Link]("Producer stopped.");

class Consumer extends Thread {

LinkedBlockingQueue<String> queue;

Consumer(LinkedBlockingQueue<String> queue) {

[Link] = queue;

public void run() {

try {

while (true) {

String msg = [Link]();

if ([Link]("Shutdown")) {

[Link]("Shutting down...");

break;

[Link]("Notification: " + msg);

} catch (InterruptedException e) {
[Link]("Consumer stopped.");

public class Main3 {

public static void main(String[] args) throws InterruptedException {

LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();

String[] events = {"User Login", "File Deleted", "Error Occurred", "Shutdown"};

Producer p = new Producer(queue, events);

Consumer c = new Consumer(queue);

[Link]();

[Link]();

[Link]();

[Link]();

OUTPUT:

You might also like