Java计数器在for循环中额外增加

时间:2018-03-24 14:38:44

标签: java eclipse multithreading

我开发了一个模拟基于队列的系统的应用程序,因此实际上模拟了一些进入和离开它们的队列和客户,就像在常规市场一样。我是为一个队列做的,现在我正在尝试为给定数量的队列做这件事。为此,我试图使用等于并行工作的队列数的线程数。

问题在于iterations,而不是1,而是2,我不明白为什么。此代码也针对1个队列运行,queue[iterations]已替换为queue[1],因为迭代为2而不是1

任何想法可能是什么问题? 谢谢!

public class Simulation {
    static volatile int iterations=1;
    public Simulation(int minArrivingTime, int maxArrivingTime, int 
minServiceTime, int maxServiceTime, int nrQueues,
        int nrCustomers, int simulationInterval) {
    Customer[] sir = new Customer[nrCustomers + 1];
    Random r = new Random();
    for (int i = 1; i <= nrCustomers; i++) { // we create a new customer, and give it an id, a random arriving time
                                                // between the specified limits and a random service time between
                                                // the specified limits
        int arrivingTime = r.nextInt(maxArrivingTime - minArrivingTime) + minArrivingTime;
        int serviceTime = r.nextInt(maxServiceTime - minServiceTime) + minServiceTime;
        sir[i] = new Customer(i, arrivingTime, serviceTime);

    }
    Queue[] queue = new Queue[nrQueues + 1];
    Thread[] thread = new Thread[nrQueues + 1];
    for (iterations = 1; iterations <= nrQueues; iterations++) {
        System.out.println(iterations);
        queue[iterations] = new Queue();
        thread[iterations] = new Thread(new Runnable() {
            public void run() {
                System.out.println(iterations);
                String result = "Queue" + iterations;

                try {
                    for (int time = 0; time <= simulationInterval; time++) // we simulate each second untill the
                                                                            // simulation is over
                    {
                        result = "Queue" + iterations;

                        for (int i = 1; i <= nrCustomers; i++) {
                            if (sir[i] != null) {
                                if (sir[i].getArrivalTime() == time) // we 
iterate trough the array of customers,
                                                                        // 
and if
                                                                        // 
their arrival time is equal to the
                                { // current time, the customer enters the 
queue
                                    queue[1].enqueue(sir[i]);
                                    sir[i]=null;
                                    // 
result=result.concat(sir[i].toString()+" ");
                                }
                            }

                        }
                        Customer first = queue[1].getQueue().element();

                        if (first.getArrivalTime() + first.getServiceTime() <= time) {
                            first.setFinishTIme(first.getArrivalTime() + first.getServiceTime());
                            queue[1].dequeue();
                        }
                        for (Customer cus : queue[1].getQueue()) {
                            result = result.concat(cus.toString() + " ");
                        }
                        System.out.println(result);
                        System.out.println(" ");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (NoSuchElementException e) {
                    result = "The queue is empty";
                    System.out.println(result);
                }

            }
        });
        thread[iterations].start();
        // System.out.println(queue[1].getqueue[1]().get(1).toString());
        /*
         * for (Customer cus : queue[1].getqueue[1]()) {
         * System.out.println(cus.toString()); }
         */
    }
}

}

0 个答案:

没有答案