Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the bellow specification I have the following code: import java.io.*; import java.util.LinkedList; import java.util.Queue; import java.util.Random; public class MyFilter { public static void main(String

Given the bellow specification I have the following code:

import java.io.*;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Random;

public class MyFilter {

public static void main(String args[]) {

SharedStorage storage = new SharedStorage(100);

int n = 100;

// Create and start producer and consumer threads

Thread producer = new Thread(new ThreadA(storage, n));

Thread consumer1 = new Thread(new ThreadK(storage, 1));

Thread consumer2 = new Thread(new ThreadK(storage, 2));

Thread consumer3 = new Thread(new ThreadK(storage, 3));

producer.start();

consumer1.start();

consumer2.start();

consumer3.start();

try {

producer.join();

consumer1.join();

consumer2.join();

consumer3.join();

} catch (InterruptedException ie) {

}

}

}

/**

* Producer thread generates numbers up to n

*/

class ThreadA implements Runnable {

private SharedStorage sharedStorage;

private Random r = new Random();

private int n;

public ThreadA(SharedStorage storage, int n){

sharedStorage = storage;

this.n = n;

}

public void run() {

for (int i=0;i

System.out.println("Generating number");

sharedStorage.insertNumber(r.nextInt(100));

}

}

}

/**

* Consumer Threads for each number k

*/

class ThreadK implements Runnable {

private SharedStorage sharedStorage;

private int k;

public ThreadK(SharedStorage storage, int k){

sharedStorage = storage;

this.k = k;

}

public void run() {

while(true) {

try {

int n = 0;

if (k >= 3) { // Print to multiples of k

System.out.println("Consumer3 number");

n = sharedStorage.removeMultipleOfK(k);

File yourFile = new File("multipleofk.txt");

yourFile.createNewFile(); // if file already exists will do nothing

BufferedWriter bw = new BufferedWriter(new FileWriter(yourFile, true));

bw.write(n + " ");

bw.close();

} else if (k == 2) { //Print to even file

System.out.println("Consumer2 number");

n = sharedStorage.removeEvenNumber();

File yourFile = new File("even.txt");

yourFile.createNewFile(); // if file already exists will do nothing

BufferedWriter bw = new BufferedWriter(new FileWriter(yourFile, true));

bw.write(n + " ");

bw.close();

} else if (k ==1 ) { // print to odd file

System.out.println("Consumer1 number");

n = sharedStorage.removeOddNumber();

File yourFile = new File("odd.txt");

yourFile.createNewFile(); // if file already exists will do nothing

BufferedWriter bw = new BufferedWriter(new FileWriter(yourFile, true));

bw.write(n + " ");

bw.close();

}

} catch (InterruptedException ie) {

} catch (IOException ioe) {

}

}

}

}

class SharedStorage {

private int m;

private Queue queue;

private int next = 0;

public SharedStorage(int capacity) {

this.m = capacity;

this.queue = new LinkedList();

}

public synchronized void insertNumber(int n) {

try {

while (queue.size() == m) {

wait();

}

queue.add(n);

notifyAll();

} catch (InterruptedException ie) {

}

}

public synchronized int removeOddNumber() throws InterruptedException {

try {

while (queue.size() == 0 || (queue.peek()== null || queue.peek() % 2 == 0)) {

wait();

}

int n = queue.poll();

notifyAll();

return n;

} catch (InterruptedException ie) {

throw ie;

}

}

public synchronized int removeEvenNumber() throws InterruptedException {

try {

while (queue.size() == 0 || (queue.peek()!= null && queue.peek() % 2 == 1)) {

wait();

}

int n = queue.poll();

notifyAll();

return n;

} catch (InterruptedException ie) {

throw ie;

}

}

public synchronized int removeMultipleOfK(int k) throws InterruptedException {

try {

while (queue.size() == 0 || (queue.peek()!= null && queue.peek() % k != 0)) {

wait();

}

int n = queue.poll();

notifyAll();

return n;

} catch (InterruptedException ie) {

throw ie;

}

}

}

My question is: How can I fix it so that a number will appear in only one of the files ? For example currently the number 33 has a chance to appear in both the odds.txt file and also the multipleofk.txt file but the specification says otherwise in the extension task.

image text in transcribed

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 disregarded. The filtering is into k files (below k is chosen to be 2). [Clearly, in a real application m and n would be several magnitudes larger] Task 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. 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 disregarded. The filtering is into k files (below k is chosen to be 2). [Clearly, in a real application m and n would be several magnitudes larger] Task 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<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions