2010-04-11 109 views
2

我的任務是模擬幾個人的活動。他們中的每一個人在一些隨機時間內執行的活動很少:快速(0-5s),中等(5-10s),慢(10-20s)和非常慢(20-30s)。每個人在同一時間獨立執行任務。在新任務開始時,我應該打印它的隨機時間,啓動任務,然後在時間過後顯示下一個任務的時間並啓動它。我寫了run()函數來計算時間,但現在看起來線程是一個接一個地完成的,而不是在同一時間完成的,或者他們只是以這種方式打印。在Java中運行多線程

public class People{ 
    public static void main(String[] args){ 
    Task tasksA[]={new Task("washing","fast"), 
       new Task("reading","slow"), 
       new Task("shopping","medium")}; 
    Task tasksM[]={new Task("sleeping zzzzzzzzzz","very slow"), 
       new Task("learning","slow"), 
       new Task(" :** ","slow"), 
       new Task("passing an exam","slow") }; 
    Task tasksJ[]={new Task("listening music","medium"), 
       new Task("doing nothing","slow"), 
       new Task("walking","medium") }; 

    BusyPerson friends[]={ new BusyPerson("Alice",tasksA), 
          new BusyPerson("Mark",tasksM), 
          new BusyPerson("John",tasksJ)}; 

    System.out.println("STARTING....................."); 
    for(BusyPerson f: friends) 
     (new Thread(f)).start(); 
    System.out.println("DONE........................."); 
    } 
} 

class Task { 

    private String task; 
    private int time; 
    private Task[]tasks; 

    public Task(String t, String s){ 
     task = t;  
     Speed speed = new Speed(); 
     time = speed.getSpeed(s); 
    } 

    public Task(Task[]tab){ 
     Task[]table=new Task[tab.length]; 
     for(int i=0; i < tab.length; i++){ 
      table[i] = tab[i]; 
     } 
     this.tasks = table; 
    } 
} 

class Speed { 

    private static String[]hows = {"fast","medium","slow","very slow"}; 
    private static int[]maxs = {5000, 10000, 20000, 30000}; 

    public Speed(){ 
    } 

    public static int getSpeed(String speedString){ 
     String s = speedString; 
     int up_limit=0; 
     int down_limit=0; 
     int time=0; 
//get limits of time 
     for(int i=0; i<hows.length; i++){ 
      if(s.equals(hows[i])){ 
       up_limit = maxs[i]; 
       if(i>0){ 
        down_limit = maxs[i-1]; 
       } 
       else{ 
        down_limit = 0; 
       } 
      } 
     } 
//get random time within the limits 
     Random rand = new Random(); 
     time = rand.nextInt(up_limit) + down_limit; 

    return time; 
    } 

} 

class BusyPerson implements Runnable { 
    private String name; 
    private Task[] person_tasks; 
    private BusyPerson[]persons; 

    public BusyPerson(String s, Task[]t){ 
     name = s; 
     person_tasks = t; 
    } 

    public BusyPerson(BusyPerson[]tab){ 
     BusyPerson[]table=new BusyPerson[tab.length]; 
     for(int i=0; i < tab.length; i++){ 
      table[i] = tab[i]; 
     } 
     this.persons = table; 
    } 

public void run() { 
    int time = 0; 
    double t1=0; 

    for(Task t: person_tasks){ 
     t1 = (double)t.time/1000; 
     System.out.println(name+" is... "+t.task+" "+t.speed+ 
       " ("+t1+" sec)"); 
     while (time == t.time) { 
      try { 
       Thread.sleep(10); 
      } catch(InterruptedException exc) { 
       System.out.println("End of thread."); 
       return; 
      } 
      time = time + 100; 
     } 
    } 

} 
} 

而且我的輸出:

STARTING..................... 
DONE......................... 
Mark is... sleeping zzzzzzzzzz   very slow   (36.715 sec) 
Mark is... learning   slow   (10.117 sec) 
Mark is...  :**    slow   (29.543 sec) 
Mark is... passing an exam   slow   (23.429 sec) 
Alice is... washing   fast   (1.209 sec) 
Alice is... reading   slow   (23.21 sec) 
Alice is... shopping   medium   (11.237 sec) 
John is... listening music   medium   (8.263 sec) 
John is... doing nothing   slow   (13.576 sec) 
John is... walking   medium   (11.322 sec) 

雖然它應該是這樣的:

STARTING..................... 
    DONE......................... 
    John is... listening music  medium  (7.05 sec) 
    Alice is... washing  fast (3.268 sec) 
    Mark is... sleeping zzzzzzzzzz  very slow (23.71 sec) 
    Alice is... reading  slow (15.516 sec) 
    John is... doing nothing slow (13.692 sec) 
    Alice is... shopping medium  (8.371 sec) 
    Mark is... learning slow (13.904 sec) 
    John is... walking  medium  (5.172 sec) 
    Mark is... :**   slow (12.322 sec) 
    Mark is... passing an exam  very slow (27.1 sec) 
+1

@owca:+1 ...很好的書面和有點酷的問題。我不明白爲什麼人們會認爲一些答案值得上調,但不要投票回答。 – SyntaxT3rr0r 2010-04-11 16:09:33

回答

3

好像你等待循環有錯誤的終止條件,使用time == t.time代替time <= t.time。此外,它似乎在每次迭代中增加100而不是10。

此外,您應該在每個循環前重置time

換句話說,循環

while (time == t.time) { 
    try { 
     Thread.sleep(10); 
    } catch(InterruptedException exc) { 
     System.out.println("End of thread."); 
     return; 
    } 
    time = time + 100; 
} 

也許應該

time = 0; 
while (time <= t.time) { 
    try { 
     Thread.sleep(10); 
    } catch(InterruptedException exc) { 
     System.out.println("End of thread."); 
     return; 
    } 
    time += 10; 
} 

,或者使用一個for循環,而不是:

for (int time = 0; time <= t.time; time += 10) { 
    try { 
     Thread.sleep(10); 
    } catch(InterruptedException exc) { 
     System.out.println("End of thread."); 
     return; 
    } 
} 

然而,真的沒有任何理由以10毫秒的增量進行睡眠。另外,Thread.sleep()可能不精確。最好一次嘗試整個過程,然後檢查時鐘。

long timeToWakeup = System.currentTimeMillis() + t.time; 
long sleepMs = t.time; 
while (sleepMs > 0) { 
    try { 
     Thread.sleep(sleepMs); 
    } catch(InterruptedException exc) { 
     System.out.println("End of thread."); 
     return; 
    } 
    sleepMs = timeToWakeup - System.currentTimeMillis()); 
} 
+0

謝謝,現在它完美的工作。 – owca 2010-04-11 14:28:27