you are going to use the semaphores for process synchronization. Therefore, you are asked to develop a consumer and producer multithreaded program.
Let’s assume, that we have a thread (producer, we will call it producer_thread) reading data (positive integer numbers) from the keyboard, entered by a user to be stored in an array (dynamic array). (Assume that the array can hold all numbers entered without overflow.)
Another thread (consumer, we will call it consumer_thread) should read data from the array and write them into a file. This thread should run concurrently with the producer (producer_thread).
Your program should make sure that the consumer_thread can read from the array only after the producer_thread has stored new data. Both threads will stop when the user enters a negative number.
Thereafter, another thread (testing_thread) should start reading the array data as well as the file data and display them on the screen in order to verify if the consumer and producer have worked in a correctly synchronized fashion. This thread should not be synchronized with other threads, it useful for testing that consumer thread is synchronized with produce thread.
What I have tried:
package multithreaded;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MultiThreaded {
public static void main(String[] args) throws InterruptedException {
Thread t1 =new Thread(new Runnable(){
@Override
public void run(){
try{
producer_thread();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
});
Thread t2 =new Thread(new Runnable(){
@Override
public void run(){
try{
consumer_thread();
}
catch (IOException ex) {
Logger.getLogger(MultiThreaded.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Thread t3 =new Thread(new Runnable(){
@Override
public void run(){
try{
testing_thread();
}
catch (IOException ex) {
Logger.getLogger(MultiThreaded.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(MultiThreaded.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t1.start();
t2.start();
t3.start();
}
private static Semaphore sem =new Semaphore(1);
private static ArrayList<Double> FromUser = new ArrayList<>();
private static void producer_thread() throws InterruptedException{
sem.acquire();
Scanner input = new Scanner(System.in);
System.out.print("Enter the numbers: ");
Double s=input.nextDouble();
while(s>=0)
{
FromUser.add(s);
s=input.nextDouble();
}
sem.release();
}
private static void consumer_thread() throws IOException {
try {
sem.acquire();
} catch (InterruptedException ex) {
Logger.getLogger(MultiThreaded.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter outputWriter = new PrintWriter("Em.txt");
for (int i = 0; i <FromUser.size(); i++) {
outputWriter.println(FromUser.get(i)+"");
}
outputWriter.close();
sem.release();
}
private static void testing_thread() throws InterruptedException, FileNotFoundException{
try {
sem.acquire();
} catch (InterruptedException ex) {
Logger.getLogger(MultiThreaded.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("the stored numbers in the ArrayList:"+FromUser);
Scanner file=new Scanner(new File("Em.txt"));
System.out.println("the stored numbers in the Text file:");
while(file.hasNextLine()){
String data=file.nextLine();
System.out.println(data);
}
sem.release();
}
}