How to return blocking queue to the right object?

211 views Asked by At

Edited hugely ;)

I have a class that starts few almost identical pattern. The way I start them:

public class Start{

public static void main(String [] args){

BlockingQueue<Rekord> Queue1;
BlockingQueue<Rekord> Queue2;
BlockingQueue<Rekord> Queue3;

for(int k=0; k < manNum; k++){
    Queue1 = new LinkedBlockingQueue<Rekord>(data.addQueue());
    Queue2 = new LinkedBlockingQueue<Rekord>(data.minQueue());
    Queue3 = new LinkedBlockingQueue<Rekord>(data.multiQueue());

    ManufacturePattern manPat=new ManufacturePattern(Queue1, Queue2, Queue3, k);
}
}
}

Inside each ManufacturePattern I start Producer, AddingMachine and MultiplyingMachine. They are all started with the same scheme:

public class ManufacturePattern{

ManufacturePattern(BlockingQueue addTaskQueue, BlockingQueue minTaskQueue, BlockingQueue multiTaskQueue, int id){
    this.addTaskQueue=addTaskQueue;
    this.minTaskQueue=minTaskQueue;
    this.multiTaskQueue=multiTaskQueue;
    this.id=id;
}
    BlockingQueue<Rekord> queueBP = new LinkedBlockingQueue<>(5);
        BlockingQueue<AddingMachine> addMachines = new ArrayBlockingQueue<>(2);
        BlockingQueue<MultiplyingMachine> multiplyingQueue = new ArrayBlockingQueue<>(2);
        BlockingQueue<MultiplyingMachine> waitMultiMachines = new ArrayBlockingQueue<>(2);

        service=Executors.newFixedThreadPool(1);
        service.submit(new Boss(queueBP, id)); //boss creates equations and puts them onto queueBP, here everything works fine


        if(numMMachines>0){
            service=Executors.newFixedThreadPool(2);
            for(int k=0; k < numMMachines; k++){
                MultiplyingMachine multi=new MultiplyingMachine(multiplyingQueue,k, id);
                multiMachines.add(multi);
                service.submit(multi);
            }
        }

        if(numAMachines>0){
            service=Executors.newFixedThreadPool(2);
            for(int k=0; k < numAMachines; k++){
                AddingMachine addM= new AddingMachine(k, id);
                addMachines.add(addM);
                service.submit(addM);
            }
        }

        service = Executors.newFixedThreadPool(2);
        for (int i=0; i < numWorkers; i++) {
            service.submit(new Producer(queueBP, addTaskQueue, minTaskQueue, multiTaskQueue, multiplyingQueue, addMachines, multiMachines, i, id));

        }
   }

So what the worker is supposed to do look like this

public class Producer implements Runnable{

public Producer(...){....}

public void run() {


while(true){
    Rekord record = queueBP.take(); 

    if(record == `+`){
     AddingMachine add = addMachines.take(); //takeAddingMachine, machine solves the problem and sends the result, `AddingMachine` is working ok
     addTaskQueue.put(result) //put the result into other queue
     addMachines.put(add);
     }
    //  cognately for `-` and `*`
}
}

The problem is when I create more then one ManufacturePattern. For some reason BlockingQueues are put back to wrong manufactures. For example worker from manufacture 0 takes adding machine, and when he puts it back on to the queue, manufacture 1 starts using them. Does anyone know why?

0

There are 0 answers