Java Assignment 2
Java Assignment 2
Set A
a) Program in define a thread for printing text on output screen for 'n' sumber
of times. Create 3 threads and run them. Pass the text 'n' parameters to the
thread constructor.
Example
Program:-
private int n;
this.text = text;
this.n = n;
@Override
1
for (int i = 0; i < n; i++) {
System.out.println(text);
thread1.start();
thread2.start();
thread3.start();
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
2
System.out.println("Thread interrupted");
}}
Outpot:-
3
b) Write a program in which themad sleep for 6 sec in the loop in reverse order
from 100 to 1 and change the name of thread.
Program:-
super(name);
@Override
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
4
}
countdownThread.start();
}Outpot:-
5
6
c)Write a program to solve producer consumer problem in which a producer
produces a value and consumer consume the value before producer generate
the next value.
Program:-
class SharedBuffer {
while (isValueProduced) {
this.value = value;
while (!isValueProduced) {
7
isValueProduced = false; // Mark that the value is consumed
return value;
this.buffer = buffer;
@Override
int i = 1;
while (true) {
try {
} catch (InterruptedException e) {
System.out.println("Producer interrupted.");
8
}
this.buffer = buffer;
@Override
while (true) {
try {
} catch (InterruptedException e) {
System.out.println("Consumer interrupted.");
9
public class Main {
producer.start();
consumer.start();
Outpot:-
10
Set B
a) Write a program to calculate the sum and average of an array of 1000
integers (generated randomly) using 10 threads. Each thread calculates
the sum of 100 integers. Use these values to calculate average. [Use join
method ].
Program:-
import java.util.Random;
this.array = array;
this.start = start;
this.end = end;
this.sum = 0;
@Override
11
for (int i = start; i < end; i++) {
sum += array[i];
return sum;
12
tasks[i] = new SumTask(array, start, end);
tasks[i].start();
// Wait for all threads to finish and calculate the total sum
long totalSum = 0;
totalSum += tasks[i].getSum();
Outpot:-
13
b) Write a program for a simple search engine. Accept a string to be
searched. Search for the string in all text files in the current folder. Use a
separate thread for each file. The result should display the filename, line
number where the string is found.
Program:-
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
this.filename = filename;
this.searchString = searchString;
@Override
String line;
int lineNumber = 1;
14
while ((line = reader.readLine()) != null) {
if (line.contains(searchString)) {
if (!found) {
found = true;
lineNumber++;
} catch (IOException e) {
15
// List all text files in the current directory
return;
executor.submit(task);
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
scanner.close();
16
Outpot:-
Program:-
import java.util.Random;
this.sharedData = sharedData;
@Override
while (true) {
try {
17
// Generate a random integer between 1 and 100
synchronized (sharedData) {
} catch (InterruptedException e) {
e.printStackTrace();
this.sharedData = sharedData;
18
@Override
while (true) {
synchronized (sharedData) {
try {
if (number % 2 == 0) {
} catch (InterruptedException e) {
e.printStackTrace();
19
class OddNumberProcessor extends Thread {
this.sharedData = sharedData;
@Override
while (true) {
synchronized (sharedData) {
try {
if (number % 2 != 0) {
20
sharedData.setNumber(-1); // Reset the number to indicate that it
has been processed
} catch (InterruptedException e) {
e.printStackTrace();
class SharedData {
private int number = -1; // -1 means no number has been generated yet
return number;
this.number = number;
21
// Create the threads
generator.start();
evenProcessor.start();
oddProcessor.start();
Outpot:-
22
23