首页 > 解决方案 > Shift Thread Start / for循环加倍?

问题描述

我试图不同时启动我的所有线程,所以当我第一次循环我的 for 循环时,我希望它们睡觉。奇怪的是,他们通过 (i == 0) 两次。我看不到什么?谢谢你的建议!这是输出:

public static Result compute(ServiceBot bot) throws InterruptedException {

       for (int i = 0; i < orderList.size(); i++) {

           System.out.println("Was ist mit i? :" + i);
           
           if (i == 0) {

               int iD = bot.getID();

               switch (iD) {
               case 0:
                   System.out.println("i: " + i + ", iD: " + iD + ", CurrentTime: " + System.currentTimeMillis());
                   break;
               case 1:
                   Thread.sleep(50);
                   System.out.println("i: " + i + ", iD: " + iD + ", CurrentTime: " + System.currentTimeMillis());
                   break;
               case 2:
                   Thread.sleep(100);
                   System.out.println("i: " + i + ", iD: " + iD + ", CurrentTime: " + System.currentTimeMillis());
                   break;
               case 3:
                   Thread.sleep(150);
                   System.out.println("i: " + i + ", iD: " + iD + ", CurrentTime: " + System.currentTimeMillis());
                   break;
               case 4:
                   Thread.sleep(200);
                   System.out.println("i: " + i + ", iD: " + iD + ", CurrentTime: " + System.currentTimeMillis());
                   break;
               }

           }

           boolean dummyBool = orderList.get(i).getUnhandled();

           if (dummyBool = true) {
               bot.doStuff();
               Thread.sleep(sleep);

           } else {
           bot.doNothing();
           }

       }

       int wait = 50;

       return new Result(wait);
   }

标签: javamultithreading

解决方案


推荐阅读