Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help me with this ? It needs to be done in Java Description To handle

ID: 3738338 • Letter: C

Question

Can anyone help me with this ? It needs to be done in Java

Description To handle a large amount of data it has been decided to filter the data into separate files. The data is larger than the local storage capacity so all the data cannot be handled at once. In this assignment we are only interested in a concurrent implementation of this filtering. So a number of simplifications have been made to the above scenario. . The data is assumed to be integers. The size of the local storage capacity is m (below m is chosen to be 4). The size of the data is n (below n is chosen to be 100m 400). Any storage needed to handle concurrency cornectly is assumed to be negligible compared to n and m, so this is d The filtering is into k files (below k is chosen to be 2). arded. [Clearly, in a real application m and n would be several magnitudes larger] lask Develop a multithreaded program with three concurrent threads, A, B and C. Thread A generates n random integer numbers and writes them into a shared memory with a capac- ity of m integers. Due to the limited capacity of the shared memory, thread A cannot write all numbers at once. Threads B and C read the integer numbers from the shared memory Thread B writes the even numbers to th file even-numbers and thread C writes the odd numbers to the file odd-numbers. Every number generated by thread A should be writ ten in one of the two files. No number should be written twice. Choose any method, or combination of methods, for synchronization. To achieve full marks: Encapsulate the synchronized methods and the shared memory into one structure. Avoid busy waiting- Avoid static variables. Extension task Write code that could easily be modified to cater for different values of m, n, and k above. For k -3you may consider the three classes of odd numbers, even numbers, and multi- ples of 3. Note that these are non-disjoint: numbers belonging to more than one class can be filtered into any of these but should still only appear in one of the output files. Partial credit for this can be achieved by describing what is needed during the viva.

Explanation / Answer

package com.chegg.problems;

public class EvenOddGenerator {

public static void main(String... args) {

int m = 4;

//Declare max numbers to write in

int n = 100 * 4;

Writer print = new Writer();

//Create Threads here

Thread t1 = new Thread(new Reader(print, n, false));

Thread t2 = new Thread(new Reader(print, n, true));

  

//Starting the threads

t1.start();

t2.start();

}

}

class Reader implements Runnable {

private int max;

private Writer print;

private boolean isEven;

Reader(Writer print, int max, boolean flagEven) {

this.print = print;

this.max = max;

this.isEven = flagEven;

}

@Override

public void run() {

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

while (number <= max) {

if (isEven)

print.printEven(number);

else

print.printOdd(number);

number += 2;

}

}

}

class Writer {

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();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote