Click here to Skip to main content
14,735,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

    }
     }
Posted
Updated 9-Apr-20 11:38am
v4
Comments
Richard MacCutchan 10-Apr-20 4:16am
   
What is the question?
Richard MacCutchan 10-Apr-20 6:27am
   
You already posted this above. Repeating it does not explain what your question is.
Member 14797538 10-Apr-20 7:04am
   
Man this is the question !!
Richard MacCutchan 10-Apr-20 7:30am
   
Yes we know that, but we have no idea why you posted it here since you have already written the code.
Member 14797538 10-Apr-20 7:48am
   
because my code is note complete, i do not have idea how to use Semaphore to synchronized the first thread (producer) with the second thread (consumer).

1 solution

Quote:
How to make 2 threads work at the same time

Short answer: threads never work at same time, same speed.
By principle, at processor level, threads never work at same time even if doing exactly the work. In an algorithm, you will always have some checkpoints where threads will have to synchronize before starting the next step.
Quote:
you are going to use the semaphores for process synchronization.

Semaphores is a method for threads to communicate.

I don't use threads, but the principles are always the same.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900