简体   繁体   中英

Keeping Track of Time in a java program

what I am trying to do is to have a process (this is just a object) with certiant values like:

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;

And I want to keep track of how much time each process gets service and how much time each process waits whle the other process gets service. I dont know if im keeping track of the time right or not I want the time in milliseconds and I think thats what it is in, im using the .nanoTime When I run the program it runs but the wait time does not increase at all but the service time for the one that is running does increase so I think I got that one right So here is how I am dong this

   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;

}

This runs the process and then calculates the service time, priority, and th wait time for each process in the linked list.

In the MyProcess class I calculate the priority like this

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

and the service time and wait time like this

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

This seams like it should work but it does not. am i missing something here Thanks for any help with this

First of all, use a long instead of an int to hold the time values, even if you divide them.

Then, you need to set the start time using System.getNanos() too, instead of setting it to 0 . Do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM