2012-10-19 145 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; 
}