【问题标题】:Keeping Track of Time in a java program在 Java 程序中跟踪时间
【发布时间】:2012-10-19 14:03:27
【问题描述】:

我想要做的是有一个进程(这只是一个对象),它的值如下:

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 类中,我这样计算优先级

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;
}

这个接缝应该可以工作,但不能。我在这里错过了什么吗 感谢您对此的任何帮助

【问题讨论】:

  • 注意:System.nanoTime() 不能保证返回长时间增加的值。在我过去的使用中,从开始时间中减去结束时间经常会导致负数。见stackoverflow.com/questions/510462/…

标签: java time linked-list


【解决方案1】:

首先,使用long 而不是int 来保存时间值,即使您将它们分开。

然后,您还需要使用System.getNanos() 设置开始时间,而不是设置为0。做类似的事情:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多