Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Suppose a router (main router) sends integer data to other 5 routers (router1, router2, router3, router4, router5) by saving the data in special buffers (5 output buffers: buffer1, buffer2, buffer3, buffer4, buffer5) located in the router itself and the other 5 routers read these data from these buffers each from its own special buffer (router1 reads from buffer1, router2 reads from buffer2, and so on). Each buffer has a limited size to 5. i.e. each buffer can save up to 5 integer numbers. Now, write a program (contains 6 threads) one thread for the main router that generates random integers and saves them in the 5 buffers randomly (it selects the buffer to save in randomly) and the other 5 threads for the 5 routers that their job is to read data from their corresponding buffers. The code has to implement the following rules:

1- The main router thread waits for 10 ms after each filling of some router of data.

2- The main router notifies all other threads that some data are written inside the buffers.

3- Other threads, after reading some data from buffers, they delete read data from the buffers.

4- The main router has to wait till a space is available in the buffers.

5- The other threads have to wait if buffers are empty (of course each thread waits for its corresponding buffer).

6- Other threads have to wait for 10 ms after each read.


What I have tried:

make array but my doctor said you can do without array
Posted
Updated 26-Jun-20 2:01am
Rate this:
Please Sign up or sign in to vote.

Solution 1

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
   
Comments
Palestinian gamer 26-Jun-20 8:01am
   
I have done most of it but am stuck on how to create 5 buffer
Rate this:
Please Sign up or sign in to vote.

Solution 2

package project.pkg2.thread;
import java.util.concurrent.*;
 import java.util.concurrent.locks.*;
public class Project2Thread {
    public static buffer buffer;

    public static void main(String[] args) {
     ExecutorService executor = Executors.newFixedThreadPool(6);  
      
       executor.execute(new mainrouter());
                        

          executor.execute(new fiverouter());
        
        
        
        executor.shutdown();
     }
    public static class mainrouter implements Runnable{
    public void run(){
    try {
          while(true){     
       
                System.out.println("main router send  " );
                 
                     
                   
                    
                     
                 
   
                 buffer.save((int) (Math.random()*10) + 1);
                            Thread.sleep( 1/100);
                }
            }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }}
    public static class fiverouter implements Runnable{ 
        
        
        public void run(){
   try{
       System.out.println("buffer read"+buffer.read());
       
       Thread.sleep( 1/100);

   }
   catch(Exception c){
   
   c.printStackTrace();
   }
          
        
        }}
   public static class buffer {
    private static final int CAPACITY = 5;  
     private java.util.LinkedList<Integer> queue =
        new java.util.LinkedList<>();
        private static Lock lock = new ReentrantLock();
        private static Condition notEmpty = lock.newCondition();
        private static Condition notFull = lock.newCondition();    
       
   
  
   public buffer(){ }
   public void save(int value) {
            
           lock.lock();  
            try {
                while ( queue.size() == CAPACITY) {
                    System.out.print(" Wait for notFull condition it is full");
                    
                    notFull.await();
                } 
                                         System.out.print(" value is "+value);
       

                 queue.offer(value);                 
                notEmpty.signalAll();  
            }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            finally {
                lock.unlock(); 
            }     
            
        }

        public int read() {
            
            
             int value = 0;
            lock.lock();  
            try {
                while ( queue.isEmpty()) {
                    System.out.println(" Wait for notEmpty condition:  it is empity");
                    notEmpty.await();
                }
                               value = queue.remove();
               
             }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            finally {
                lock.unlock();  
               
            }
            return value; 
}}}
   
Comments
Richard MacCutchan 26-Jun-20 8:40am
   
If this is part of your question then please add it to the actual question and delete this Solution.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100