Week 7 – Tutorial Assignment
CONCURRENT PROGRAMMING PARADIGM: THREAD CLASSES AND METHODS
RA2311026010866
1. import [Link];
class RandomNumberGenerator extends Thread {
private final NumberProcessor processor;
public RandomNumberGenerator(NumberProcessor processor) {
[Link] = processor;
public void run() {
Random random = new Random();
while (true) {
int number = [Link](100); // Generate random integer between 0 and 99
[Link]("Generated Number: " + number);
[Link](number);
try {
[Link](1000); // Sleep for 1 second
} catch (InterruptedException e)
{ [Link]().interrupt();
}
}
class EvenNumberProcessor extends Thread {
private final NumberProcessor processor;
public EvenNumberProcessor(NumberProcessor processor) {
[Link] = processor;
public void run() {
while (true) {
Integer number = [Link]();
if (number != null) {
int square = number * number; [Link]("Square
of " + number + ": " + square);
class OddNumberProcessor extends Thread {
private final NumberProcessor processor;
public OddNumberProcessor(NumberProcessor processor) {
[Link] = processor;
}
public void run() {
while (true) {
Integer number = [Link]();
if (number != null) {
int cube = number * number * number;
[Link]("Cube of " + number + ": " + cube);
class NumberProcessor {
private Integer evenNumber = null;
private Integer oddNumber = null;
public synchronized void processNumber(int number) {
if (number % 2 == 0) {
evenNumber = number;
notifyAll(); // Notify threads that a new even number is available
} else {
oddNumber = number;
notifyAll(); // Notify threads that a new odd number is available
}
public synchronized Integer getEvenNumber() {
while (evenNumber == null) {
try {
wait(); // Wait for an even number to be available
} catch (InterruptedException e)
{ [Link]().interrupt();
Integer number = evenNumber;
evenNumber = null; // Reset the even number
return number;
public synchronized Integer getOddNumber() {
while (oddNumber == null) {
try {
wait(); // Wait for an odd number to be available
} catch (InterruptedException e)
{ [Link]().interrupt();
Integer number = oddNumber;
oddNumber = null; // Reset the odd number
return number;
}
public class Main {
public static void main(String[] args) {
NumberProcessor processor = new NumberProcessor();
RandomNumberGenerator generator = new RandomNumberGenerator(processor);
EvenNumberProcessor evenProcessor = new EvenNumberProcessor(processor);
OddNumberProcessor oddProcessor = new OddNumberProcessor(processor);
[Link]();
[Link]();
[Link]();
2. import [Link];
import [Link];
class Producer extends Thread { private
final Queue<Integer> queue; private
final int maxSize;
public Producer(Queue<Integer> queue, int maxSize) {
[Link] = queue;
[Link] = maxSize;
public void run()
{ int value = 0;
while (true) {
synchronized (queue) {
while ([Link]() == maxSize) {
try {
[Link](); // Wait until space is available
} catch (InterruptedException e)
{ [Link]().interrupt();
[Link]("Produced: " + value);
[Link](value++);
[Link](); // Notify the consumer that a new item is available
}
}
class Consumer extends Thread {
private final Queue<Integer> queue;
public Consumer(Queue<Integer> queue) {
[Link] = queue;
public void run() {
while (true) {
synchronized (queue) {
while ([Link]()) {
try {
[Link](); // Wait until an item is available
} catch (InterruptedException e)
{ [Link]().interrupt();
int value = [Link]();
[Link]("Consumed: " + value);
[Link](); // Notify the producer that space is available
}
}
public class Main {
public static void main(String[] args)
{ Queue<Integer> queue = new LinkedList<>();
int maxSize = 5;
Producer producer = new Producer(queue, maxSize);
Consumer consumer = new Consumer(queue);
[Link]();
[Link]();
}
3. class MyThread extends Thread {
public void run() {
[Link]("Original Thread Name: " + [Link]().getName());
try {
[Link](5000); // Sleep for 5 seconds
} catch (InterruptedException e)
{ [Link]().interrupt();
} [Link]().setName("ChangedThreadName");
[Link]("Changed Thread Name: " + [Link]().getName());
}
public class Main {
public static void main(String[] args)
{ MyThread thread = new
MyThread(); [Link]();
4. class CountdownThread extends Thread {
public void run() {
for (int i = 5; i >= 1; i--)
{ [Link]("Count: " + i);
try {
[Link](6000); // Sleep for 6 seconds
} catch (InterruptedException e)
{ [Link]().interrupt();
} [Link]().setName("CountdownCompleteThread");
[Link]("Changed Thread Name: " + [Link]().getName());
public class Main {
public static void main(String[] args) { CountdownThread
thread = new CountdownThread(); [Link]();
5. class UserThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++)
{ [Link]("User Thread: " + i);
try {
[Link](1000); // Sleep for 1 second
} catch (InterruptedException e)
{ [Link]().interrupt();
public class Main {
public static void main(String[] args)
{ UserThread userThread = new UserThread();
[Link](); // Start the user thread
for (int i = 0; i < 5; i++)
{ [Link]("Main Thread: " + i);
try {
[Link](1000); // Sleep for 1 second
} catch (InterruptedException e)
{ [Link]().interrupt();
6. class Printer {
private final Object lock = new Object();
private int currentJob = 1;
public void printJob(int jobNumber) {
synchronized (lock) {
while (jobNumber != currentJob) {
try {
[Link](); // Wait for the correct job order
} catch (InterruptedException e)
{ [Link]().interrupt();
[Link]("Printing Job: " + jobNumber);
currentJob++;
[Link](); // Notify the next job
class JobThread extends Thread
{ private final Printer printer;
private final int jobNumber;
public JobThread(Printer printer, int jobNumber) {
[Link] = printer;
[Link] = jobNumber;
public void run() {
[Link](jobNumber);
}
public class Main {
public static void main(String[] args)
{ Printer printer = new Printer();
// Creating job threads in a non-sequential order
JobThread job1 = new JobThread(printer, 1);
JobThread job2 = new JobThread(printer, 2);
JobThread job3 = new JobThread(printer, 3);
JobThread job4 = new JobThread(printer, 4);
JobThread job5 = new JobThread(printer, 5);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
7. class ThreadA extends Thread {
private final String str;
private int digitCount;
public ThreadA(String str) {
[Link] = str;
public void run() {
for (char ch : [Link]()) {
if ([Link](ch)) {
digitCount++;
[Link]("ThreadA: " + digitCount);
class ThreadB extends Thread {
private final String str;
private int charCount;
public ThreadB(String str) {
[Link] = str;
public void run() {
for (char ch : [Link]()) {
if ([Link](ch)) {
charCount++;
[Link]("ThreadB: " + charCount);
public class Main {
public static void main(String[] args)
{ String k = "Hello123World456";
ThreadA threadA = new ThreadA(k);
ThreadB threadB = new ThreadB(k);
[Link]();
[Link]();
8. import [Link];
class UserThreadPriority extends Thread {
private String k;
private char c;
public UserThreadPriority(String k, char c) {
this.k = k;
this.c = c;
public void run() {
[Link](getName() + ": String = " + k + ", Character = " + c);
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String inputString = [Link]();
[Link]("Enter a character: ");
char inputChar = [Link]().charAt(0);
UserThreadPriority threadobj1 = new UserThreadPriority(inputString, inputChar);
UserThreadPriority threadobj2 = new UserThreadPriority(inputString, inputChar);
[Link]("ThreadA");
[Link]("ThreadB");
[Link]();
[Link]();
[Link]();
9. class SleepThread extends Thread {
private final long sleepTime;
public SleepThread(long sleepTime) {
[Link] = sleepTime;
public void run() {
try {
[Link](getName() + " will sleep for " + sleepTime + " nanoseconds.");
[Link](sleepTime / 1_000_000); // Convert nanoseconds to milliseconds
[Link](getName() + " has woken up.");
} catch (InterruptedException e)
{ [Link]().interrupt();
public class Main {
public static void main(String[] args) { SleepThread
thread1 = new SleepThread(10); SleepThread
thread2 = new SleepThread(20); SleepThread
thread3 = new SleepThread(50); SleepThread
thread4 = new SleepThread(70); SleepThread
thread5 = new SleepThread(100);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
10. class PriorityThread extends Thread {
public PriorityThread(String name) {
super(name);
public void run() {
[Link](getName() + " with priority " + getPriority() + " is executing.");
public class Main {
public static void main(String[] args) {
PriorityThread thread1 = new PriorityThread("Thread 1");
PriorityThread thread2 = new PriorityThread("Thread 2");
PriorityThread thread3 = new PriorityThread("Thread 3");
PriorityThread thread4 = new PriorityThread("Thread 4");
PriorityThread thread5 = new PriorityThread("Thread 5");
// Set thread priorities
[Link](Thread.MIN_PRIORITY); // 1
[Link](Thread.NORM_PRIORITY); // 5
[Link](Thread.MAX_PRIORITY); // 10
[Link](Thread.MIN_PRIORITY); // 1
[Link](Thread.NORM_PRIORITY); // 5
// Start threads
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();