title: multiple queue waiting time
body: <p>There are more than 2 queues in the system.</p>
  
  <p><img src="https://i.stack.imgur.com/kepTh.png" alt="enter image description here"></p>
  
  <p>Packets following the exponential distribution(lamda, mu) continue to enter each queue(queue1, queue2)</p>
  
  <p>However, the service rate mu is processed once for queue1, one for queue2, onece queue1, and once for queue2.</p>
  
  <p>That is, when processing a packet in queue1, the waiting time of queue2 increases. and the waiting time of queue1 increases when processing queue2.</p>
  
  <p>At this time, how do I calculate the waiting time in queue1 and queue2?</p>
  
  <p>I use a java language.</p>
  
  <p>lamda is arrival rate, mu is service rate, how do I get the waiting time through this?</p>
  
  <p>Because of this, I'm leaking a few nights, please help me...</p>
  
  <pre><code>    package scheduling;
  
      import java.util.LinkedList;
      import java.util.Random;
  
      public class q_learning_final2 {
  
  private final double alpha = 0.1;
  private final double gamma = 0.9;
  private static final double rqstate = 0.2;
  private static final double rqstate2 = 0.0;
  private static final double rqstate3 = -0.2;
  
  private final double rwtime = 0;
  private final double rwtime2 = 0;
  private final double rwtime3 = 0;
  private static double num = 5.0;
  private static double num1 = 5.0;
  static int cnt=0;
  public static class State {
      public double lambda;
      public double ts;
      public double simulationTime;
  
      public State(double lam, double ts, double simTime) {
          this.lambda = lam;
          this.ts = ts;
          this.simulationTime = simTime;
      }
  }
  
  public static class State2 {
      public double lambda;
      public double ts;
      public double simulationTime;
  
      public State2(double lam, double ts, double simTime) {
          this.lambda = lam;
          this.ts = ts;
          this.simulationTime = simTime;
      }
  }
  
  public static class State3 {
      public double lambda;
      public double ts;
      public double simulationTime;
  
      public State3(double lam, double ts, double simTime) {
          this.lambda = lam;
          this.ts = ts;
          this.simulationTime = simTime;
      }
  }
  
  public static class Event {
      public int id;
      public double interarrival;
      public double arrival;
      public double service;
      public double serviceStart;
      public double serviceEnd;
      public double tw;
      public double tq;
  
      public void function(LinkedList&lt;Event&gt; schedule, State state, double time) {
          Event next = schedule.removeLast();
          if (this.serviceEnd &lt; next.arrival) {
              next.serviceStart = next.arrival;
          }
          if (this.serviceEnd &gt;= next.arrival) {
              next.serviceStart = this.serviceEnd;
          }
          next.serviceEnd = next.serviceStart + next.service;
  
          if(cnt==0) {
              next.tw = next.serviceStart ;//is this correct?
              System.out.println(next.tw);
              cnt=1;
  
          }else if(cnt==1) {
              next.tw = next.serviceStart-next.arrival ;//is this correct?
  
              cnt=0;
  
          }
          next.tq = next.serviceEnd - next.arrival;           
          schedule.addLast(next);
      }
  }
  
  public static class Event2 {
      public int id;
      public double interarrival;
      public double arrival;
      public double service;
      public double serviceStart;
      public double serviceEnd;
      public double tw;
      public double tq;
  
      public void function(LinkedList&lt;Event2&gt; schedule2, State state2, double time2) {
          Event2 next = schedule2.removeLast();
          if (this.serviceEnd &lt; next.arrival) {
              next.serviceStart = next.arrival;
  
          }
          if (this.serviceEnd &gt;= next.arrival) {
              next.serviceStart = this.serviceEnd;
  
          }
          next.serviceEnd = next.serviceStart + next.service;
          if(cnt==0) {
              next.tw = next.serviceStart - next.arrival;//여기 건드려야해
              cnt=1;
          }else if(cnt==1) {
              next.tw = next.serviceStart;//여기 건드려야해
              cnt=0;
          }
  
          next.tq = next.serviceEnd - next.arrival;
  
          schedule2.addLast(next);
      }
  }
  
  
  public static double exponential(double lambda) {
      Random R = new Random();
      return -(Math.log(1 - R.nextDouble()) / lambda);
  }
  
  public static State initializeState(double lambda, double ts, double simTime) {
      System.out.println("Lambda\tTs\tSimulationTime");
      System.out.println(lambda + "\t" + ts + "\t" + simTime);
      State state = new State(lambda, ts, simTime);
      return state;
  }
  
  public static State initializeState2(double lambda, double ts, double simTime) {
      System.out.println("Lambda\tTs\tSimulationTime");
      System.out.println(lambda + "\t" + ts + "\t" + simTime);
      State state2 = new State(lambda, ts, simTime);
      return state2;
  }
  
  
  public static LinkedList&lt;Event&gt; initializeSchedule(State state) {
      LinkedList&lt;Event&gt; schedule = new LinkedList&lt;Event&gt;();
      double time = 0.0;
      for (int i = 1; i &lt; 5000; i++) {
          Event event = new Event();
          event.id = i;
          event.interarrival = exponential(state.lambda);
          event.service = exponential(state.ts);
          time += event.interarrival;
          event.arrival = time;
          schedule.push(event);
  
      }
      return schedule;
  }
  
  public static LinkedList&lt;Event2&gt; initializeSchedule2(State state2) {
      LinkedList&lt;Event2&gt; schedule2 = new LinkedList&lt;Event2&gt;();
      double time = 0.0;
      for (int i = 1; i &lt; 10000; i++) {
          Event2 event2 = new Event2();
          event2.id = i;
          event2.interarrival = exponential(state2.lambda);
  
          event2.service = exponential(state2.ts);
          time += event2.interarrival;
          event2.arrival = time;
          schedule2.push(event2);
  
      }
      return schedule2;
  }
  
  public static void printResults(double wait, double turnaround, int count, State state, double time) {
      double tw = wait / count;
      double tq = turnaround / count;
  
      System.out.println("\nResults of Queue 1");
      System.out.println("Number of Events Processed:\t" + count);
      System.out.println("Average Wait Time in Queue 1:\t" + tw);
      System.out.println("Average Time in System 1:\t" + tq);
  
  }
  
  public static void printResults2(double wait2, double turnaround2, int count2, State state2, double time2) {
      double tw2 = wait2 / count2;
      double tq2 = turnaround2 / count2;
  
      System.out.println("\nResults of Queue 2");
      System.out.println("Number of Events Processed:\t" + count2);
      System.out.println("Average Wait Time in Queue 2:\t" + tw2);
      System.out.println("Average Time in System 2:\t" + tq2);
  
  }
  
  public static void main(String[] args) {
  
      State state = initializeState(4.0, 10.0, 100);
      State state2 = initializeState2(3.0, 10.0, 100);
  
      LinkedList&lt;Event&gt; schedule = initializeSchedule(state);
      LinkedList&lt;Event2&gt; schedule2 = initializeSchedule2(state2);
  
  
      int count = 0;
      int count2 = 0;
      int count3 = 0;
  
      double time = 0.0;
      double time2 = 0.0;
      double time3 = 0.0;
  
      double wait = 0.0;
      double wait2 = 0.0;
      double wait3 = 0.0;
  
      double turnaround = 0.0;
      double turnaround2 = 0.0;
      double turnaround3 = 0.0;
  
      while (time &lt; state.simulationTime) {
          Event event = schedule.removeLast();
          if (time == 0.0) {
              event.serviceStart = event.interarrival;
              event.serviceEnd = event.serviceStart + event.service;
              event.tw = event.serviceStart - event.arrival;
              event.tq = event.serviceEnd - event.arrival;
          }
          event.function(schedule, state, time);
          count++;
          time = event.arrival;
          wait += event.tw;
          turnaround += event.tq;
  
  
      }
  
      while (time2 &lt; state2.simulationTime) {
          Event2 event2 = schedule2.removeLast();
          if (time2 == 0.0) {
              event2.serviceStart = event2.interarrival;
              event2.serviceEnd = event2.serviceStart + event2.service;
              event2.tw = event2.serviceStart - event2.arrival;
              event2.tq = event2.serviceEnd - event2.arrival;
          }
          event2.function(schedule2, state2, time2);
          count2++;
          time2 = event2.arrival;
          wait2 += event2.tw;
          turnaround2 += event2.tq;
  
      }
  
      printResults(wait, turnaround, count, state, time);
      printResults2(wait2, turnaround2, count2, state2, time2);
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And I wrote it in the source code is this correct?, is this correct?..</p>