2012-10-19 75 views
0

什麼,我試圖做的是有一個過程(這只是一個對象)與像certiant值:保持時間的跟蹤Java程序

public String name; 
public int priority; //not to exceed 4 
public int serviceTimeTotal; //holds the cumlitive service time 
public int waitTimeTotal; //holds the cumulative wait time 
public int arrivalTime; 
public int burstTime; 
public boolean added; 
public boolean active; 
public boolean isDone; 

我想跟蹤多少每個進程獲得服務的時間以及每個進程等待另一個進程獲得服務的時間。我不知道是否正在跟蹤時間正確與否我希望以毫秒爲單位的時間,我認爲它是什麼,即時通訊使用.nanoTime當我運行該程序時,它運行但等待時間根本不增加,但服務時間爲運行在一個確實增加,所以我想我得到了一個正確的 所以在這裏是怎麼了東本

public static void runLevelOne(LinkedList<MyProcess> ll, MyProcess mp) 
{ 
    int start = 0; 

    mp.active = true; 
    System.out.println("Running Level One Queue"); 
    //runs for 3 millseconds 
    while(start <= 3000) 
    { 
     //cheak to see if process is over 
     if(mp.burstTime <= mp.serviceTimeTotal) 
     { 
      mp.isDone = true; 
      System.out.println(mp.name + " has completed its task"); 
      mp.active = false; 
      //get rid of it from the queue 
      ll.remove(mp); 
      break; 

     } 


     try { 
      //run proccess 
      Thread.sleep(3); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(PartATwo.class.getName()).log(Level.SEVERE, null, ex); 
     } 


     start += System.nanoTime()/ 1000000; 
     mp.serviceTimeCalculator(start); 
     mp.calculatePriority(); 
     //make all other proccesses in ll wait 
     for(MyProcess s :ll) 
     { 
      s.waiting(start); 
      System.out.println(s.name+ " wait time: " + s.waitTimeTotal); 
     } 
     System.out.println(mp.name + " new priority is: " + mp.priority); 
    } 
    mp.active = false; 

} 

此運行過程,然後計算服務時,優先級和次等待鏈接列表中每個進程的時間。

在MyProcess I類計算的優先級這樣

public int calculatePriority() 
{ 
    System.out.println("Starting Priority " + priority); 
    System.out.println("service time " + serviceTimeTotal); 
    System.out.println("waitTimeTotal " + waitTimeTotal); 

    priority = (serviceTimeTotal + waitTimeTotal)/serviceTimeTotal; 
    if(priority <= 1.5) 
    { 
     priority = 1; 
    } 
    if(priority > 1.6 && priority <= 2.5) 
    { 
     priority = 2; 
    } 
    if(priority > 2.6 && priority <= 3.5) 
    { 
     priority = 3; 
    } 
    if(priority > 3.6 && priority > 3.5) 
    { 
     priority = 4; 
    } 
    return priority; 
} 

,服務時間和等待時間這樣

public void waiting(int time) 
{ 
    if(active = false) 
    { 
     waitTimeTotal += time; 
    } 
} 
public void serviceTimeCalculator(int time) 
{ 
    serviceTimeTotal += time; 
} 

這接縫像它應該工作,但事實並非如此。我失去了一些東西在這裏 感謝這個

+0

注意:System.nanoTime()不保證返回長時間增加的值。在我過去的使用中,從開始時間減去結束時間常常導致負數。見http://stackoverflow.com/questions/510462/is-system-nanotime-completely-useless – Zagrev

回答

1

首先的任何幫助,請使用long而不是int保存時間值,即使你把他們。

然後,您還需要使用System.getNanos()來設置開始時間,而不是將其設置爲0。做類似:

long start = System.getNanos()/1000000; 
long end = start; 
while (end - start < 3000) { 
    ... 
    end = System.getNanos()/1000000; 
}