我的变量值保持不变,并且不会增加

时间:2019-12-29 10:41:17

标签: java concurrency

我要在此处进行的操作是,一旦我的boxingQ.size()达到27,就将其包装到一个盒子中,然后搬到装卸区,但是我发现这有点困难,因为我希望每次大小达到27时执行此操作,但是我不确定如何为该语句编写循环。而且,我想知道一旦它装满一个盒子,我该如何使盒子不增加,以便一旦它达到12,它将继续到下一个海湾?

public class AsiaPacificFruitFactory {


public static void main(String[] args) {
    LinkedBlockingQueue<Can>beltQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Can>sterilizeQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Can>sterilize1Q = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Can>fillingQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Can>sealingQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Can>labellingQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Box>packagingQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Bigbox>boxingQ = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<LoadingBay>loadingQ = new LinkedBlockingQueue<>();

    Belt b = new Belt(beltQ);
    b.start();
    Sterilize s = new Sterilize(beltQ, sterilizeQ);
    s.start();
    Sterilize1 s1 = new Sterilize1(sterilizeQ, sterilize1Q);
    s1.start();
    Filling f = new Filling (sterilize1Q, fillingQ);
    f.start();
    Sealing seal = new Sealing (fillingQ, sealingQ);
    seal.start();
    Labelling l = new Labelling (sealingQ, labellingQ);
    l.start();
    Packaging p = new Packaging (labellingQ, packagingQ);
    p.start();
    Boxing box = new Boxing (packagingQ, boxingQ);
    box.start(); 
    LoadingArea la = new LoadingArea (boxingQ, loadingQ);
    la.start();
} 
}

class Bigbox {
    Box b;
    int BigboxNo;

    public Bigbox (Box b, int bigboxNo){
        this.b = b;
        this.BigboxNo = bigboxNo;
    }
}

class LoadingBay {
    Bigbox bb;
    int BayNo;

    public LoadingBay (Bigbox bb, int bayNo){
        this.bb = bb;
        this.BayNo = bayNo;
    }
}

class LoadingArea extends Thread {
    LinkedBlockingQueue<Bigbox>boxingQ;
    LinkedBlockingQueue<LoadingBay>loadingQ;

    public LoadingArea (LinkedBlockingQueue<Bigbox>boxingQ, LinkedBlockingQueue<LoadingBay>loadingQ){
        this.boxingQ = boxingQ;
        this.loadingQ = loadingQ;
    }

    synchronized public void run(){
        int bayNo = 1;
        while(true){
            try{
                if(boxingQ.size() == 27 ){

                    System.out.println("Loading boxes into bay area");
                    Bigbox big = boxingQ.take();
                    LoadingBay lb = new LoadingBay(big, bayNo);
                    loadingQ.put(lb);
                    System.out.println("Box " + big.BigboxNo + " has been loaded into loading bay " + lb.BayNo);
                    big.BigboxNo++;
                    if(big.BigboxNo > 12){
                        big.BigboxNo = 1;
                    }
                    if(loadingQ.size() > 12){
                        bayNo++;
                    }
                }

            }catch(Exception e){}
        }
    }
}

我的输出是这样的

Box 1 has been loaded into loading bay 1
Packet 2 have been loaded into box 2
Box 1 has been loaded into loading bay 1
Packet 3 have been loaded into box 2

如您所见,我不确定如何编写有关数据包到达27的位置的声明,然后该盒子将被转移到装货区,而不是现在每次将数据包装入盒子时,箱将进入装货区

0 个答案:

没有答案