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 {

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education