代码之家  ›  专栏  ›  技术社区  ›  user3485189

生产者/消费者-计算平均时间

  •  0
  • user3485189  · 技术社区  · 11 年前

    我有四门课-生产者,消费者,商店,主。

    试图计算顾客排队等待进入商店的平均等待时间。

    我在Producer中的运行方法-

    public void run() {
      for (int i = 1; i < size; i++) {
    
         shop.put(i);
         System.out.println("Customer number "  + i + " has entered the store\n");
         enterStore = System.currentTimeMillis();
         try {
            Thread.sleep((int) (Math.random() * 1000));
    
         } catch (InterruptedException e) { }
      }
    }
    

    我在商店的put方法的一部分-

    System.out.println("The store is full! Customer " + value + " is waiting in the queue.....\n");
            queueStart = System.currentTimeMillis();
            System.out.println("Customer joined queue at time : " + queueStart);
    
            wait();
    

    也是我的一部分-

    double averageTime = (Shop.queueStart - Producer.enterStore)/20;
      System.out.println("Average time customer queued outside store = " + averageTime);
    

    我的代码中的其他一切都正常工作,但平均值会打印一个负数,所以我显然在某个地方出了问题。我对java真的很陌生,只是想寻求一些帮助!

    谢谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   Warlord    11 年前

    时间越晚 double 值,因此需要切换减法操作数,以便

    Producer.enterStore - Shop.queueStart