Week 7 Assignment
Week 7 Assignment
RA2311026010866
1. import java.util.Random;
this.processor = processor;
while (true) {
processor.processNumber(number);
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
}
}
this.processor = processor;
while (true) {
if (number != null) {
this.processor = processor;
}
public void run() {
while (true) {
if (number != null) {
class NumberProcessor {
if (number % 2 == 0) {
evenNumber = number;
} else {
oddNumber = number;
}
public synchronized Integer getEvenNumber() {
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
return number;
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
return number;
}
public class Main {
generator.start();
evenProcessor.start();
oddProcessor.start();
2. import java.util.LinkedList;
import java.util.Queue;
this.queue = queue;
this.maxSize = maxSize;
{ int value = 0;
while (true) {
synchronized (queue) {
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
queue.add(value++);
}
}
this.queue = queue;
while (true) {
synchronized (queue) {
while (queue.isEmpty()) {
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
}
}
int maxSize = 5;
producer.start();
consumer.start();
}
3. class MyThread extends Thread {
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
} Thread.currentThread().setName("ChangedThreadName");
}
public class Main {
MyThread(); thread.start();
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
} Thread.currentThread().setName("CountdownCompleteThread");
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
6. class Printer {
synchronized (lock) {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
currentJob++;
this.printer = printer;
this.jobNumber = jobNumber;
printer.printJob(jobNumber);
}
public class Main {
job1.start();
job3.start();
job2.start();
job5.start();
job4.start();
this.str = str;
if (Character.isDigit(ch)) {
digitCount++;
this.str = str;
if (Character.isAlphabetic(ch)) {
charCount++;
{ String k = "Hello123World456";
threadA.start();
threadB.start();
8. import java.util.Scanner;
class UserThreadPriority extends Thread {
private String k;
private char c;
this.k = k;
this.c = c;
threadobj2.setName("ThreadB");
threadobj1.start();
threadobj2.start();
scanner.close();
this.sleepTime = sleepTime;
try {
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
}
10. class PriorityThread extends Thread {
super(name);
thread2.setPriority(Thread.NORM_PRIORITY); // 5
thread3.setPriority(Thread.MAX_PRIORITY); // 10
thread4.setPriority(Thread.MIN_PRIORITY); // 1
thread5.setPriority(Thread.NORM_PRIORITY); // 5
// Start threads
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();