Question
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 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started