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 {
// 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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F1f1fd054-0c44-4879-b08e-425a6935829a%2Fc9167512-5129-40e6-8ab8-1a518836528b%2Fpo6idad_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
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();
}
}
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)