java - 在 Java 程序中跟踪时间

标签 java time linked-list

我想做的是拥有一个具有确定值的进程(这只是一个对象),例如:

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

这看起来应该可以工作,但事实并非如此。我在这里错过了什么吗 感谢您对此的任何帮助

最佳答案

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

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

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

关于java - 在 Java 程序中跟踪时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975964/

相关文章:

java - 是否可以在 Mersenne Twister RNG (Java) 中指定 nextInt 的上限和下限

java - 引用计数是一个好的设计吗

java - 何时清理ReferenceQueue?被系统清理了?

c - 在c中获取日期和时间

java - 如何获取用于制作大字节数组的实际字节?

java - 为什么java程序输出以下内容

macos - Mac OS X 上是否存储了闰秒?

c - 关于在链表末尾添加元素的问题

java - 将文件内容复制到链表数组中并对其进行排序

c++ - 如何删除 C++ 链表中的节点?