ivate boolean isEvenNumber; public CheckEvenOdd(Printer printer,int maxLimit,boolean isEvenNumber) { this.maxLimit = maxLimit; this.isEvenNumber = isEvenNumber; this.printer = printer; } @Override public void run() { int number = isEvenNumber ? 2 : 1; while (number <= maxLimit) { if (isEvenNumber) { printer.printEven(number); } else {

Systems Architecture
7th Edition
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Stephen D. Burd
Chapter11: Operating Systems
Section: Chapter Questions
Problem 5RQ
icon
Related questions
Question

// the language is java

according to this code, do the next steps.

/class for check even or odd number

class CheckEvenOdd implements Runnable {

private int maxLimit;

private Printer printer;

private boolean isEvenNumber;

public CheckEvenOdd(Printer printer,int maxLimit,boolean isEvenNumber) {

this.maxLimit = maxLimit;

this.isEvenNumber = isEvenNumber;

this.printer = printer;

}

@Override

public void run() {

int number = isEvenNumber ? 2 : 1;

while (number <= maxLimit) {

if (isEvenNumber) {

printer.printEven(number);

} else {

printer.printOdd(number);

}

number += 2;

}

}

}

//Printer class for print the number with thread name

class Printer {

private volatile boolean isOdd;

synchronized void printEven(int number) {

while (!isOdd) {

try {

wait();

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

}

}

System.out.println(Thread.currentThread().getName() + " thread :" + number);

isOdd = false;

notify();

}

synchronized void printOdd(int number) {

while (isOdd) {

try {

wait();

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

}

}

System.out.println(Thread.currentThread().getName() + " thread :" + number);

isOdd = true;

notify();

}

}

//Main class contains two threads.

public class Main{

public static void main(String[] args) {

Printer print = new Printer();

Thread t1 = new Thread(new CheckEvenOdd(print, 50, false),"Odd");

Thread t2 = new Thread(new CheckEvenOdd(print, 50, true),"Even");

t1.start();

t2.start();

}

}

a. Assign each thread a different priority. Run the code and observe the execution result.
Save all files.
501
b. Assign different sleeping times to each thread using the sleep() method to alternate
execution between the two threads. Remove the existing yield() method. Run the code
and observe the execution result. Save all files.
Transcribed Image Text:a. Assign each thread a different priority. Run the code and observe the execution result. Save all files. 501 b. Assign different sleeping times to each thread using the sleep() method to alternate execution between the two threads. Remove the existing yield() method. Run the code and observe the execution result. Save all files.
Expert Solution
Step 1

public class PrintEvenOddTeaser {

class TaskEvenOdd implements Runnable {

private int max;

private printer print;

private boolean isEvenNumber;

 

TaskEvenOdd(Printer print, int max , boolean isEvenNumber) {

this.print = print;

this.max=max;

this.isEvenNumber = isEvenNumber;

}

@Override

public void run(){

//System.out.println("Run method");

int number = isEvenNumber==true ? 2:1;

while(number <=max){

if(is EvenNumber) {

//System.out.println("Even :"+Thread.currentThread().getName());

print.printEven(number);

//number+=2;

}

else {

//System.out.println("Odd:"+Thread.currentThread().getName());

print.printOdd(number);

//number+=2;

}

number +=2;

}

}

}

class Printer{

boolean isOdd=false;

synchronized void printEven(int number) {

while(isOdd==false) {

try {

wait();

}

catch(InterruptedException e) {

e.printStackTrace();

}

}

System.out.println("Even:"+number) {

isOdd==false;

notifyAll();

}

synchronized void printOdd(int number){

while(isOdd== true){
try {

wait();

}

catch(InterruptedException e) {

e.printStackTrace();

}

}

System.out.println("Odd:" + number);

isOdd=true;

notifyAll();

}

}

 

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning