The principle of implementing queuing theory in Java _java

Source: Internet
Author: User
Tags static class time interval

Introduced:

Some time ago to the bank to do business, line of people that is really much, their formal business also less than 5 minutes, but waited for two hours (I believe many people have encountered this situation), the level of service is really silent, but the problem has come, banks should open a few windows, not only to ensure the overall quality of service, Can we guarantee the utilization of resource resources? Here we will simulate this problem by queuing theory.

A brief introduction to queuing theory

Queuing theory is the mathematical theory and method of stochastic agglomeration and stochastic system work engineering, also called Stochastic service system theory, which is a branch of operational research. Let's simplify the queue theory below and look at the following figure:

We arrange a number of blue desks on the left side of the map, the right side for the red customers may come over, the middle of the yellow waiting area, if there is a desk in the idle state, customers can directly to accept services, otherwise it is necessary to wait in the yellow area, customer service in the order of the first Now if we know the probability distribution of the customer, then we will arrange a few desks on the left side to achieve better service level, but also to ensure the use of the help desk? Let's simulate the problem by building the model below.

The step-by-Step realization of queuing theory

1 for queuing theory, we first have to determine customer attributes, know when customers arrive, the required service time, and so on, we first create a customer class, where we specify the maximum customer service, the minimum time, here we in order to simplify the direct view of the service is completely random:

public class Customerbean { 
 //min service time 
 private static int minservetime = 3 * 1000; 
 Maximum service time 
 private static int maxservetime = * 1000; 
 The customer reaches the time 
 private long arrivetime; 
 The customer needs the service to be time-consuming 
 private int servetime; 
  
 Public Customerbean () { 
  //Setting arrival time 
  Arrivetime = System.currenttimemillis (); 
  Randomly set the customer's service time 
  servetime = (int) (Math.random () * (maxservetime-minservetime) + minservetime); 
 } 
 

2 above we define the customer, then we need to define a queue, and we'll look at the attributes of the queue, where we define an array to keep the queues of customers, define the minimum, maximum time interval for the next customer arrival, and the probability that the customer will not come. If the next customer's interval is 3, but is calculated and satisfied by the probability, the customer does not enter the queue, and the reason for this setting is to make the customer as large as possible random and the largest number of queues in the queue.

public class Customerquene { 
 //Waiting for customer queue 
 private linkedlist<customerbean> customers = new linkedlist< Customerbean> (); 
 Next customer. Shortest time 
 private int mintime = 0; 
 Next customer. Maximum time 
 private int maxtime = 1 * 1000; 
 Probability of coming to the customer 
 private double rate = 0.9; 
 Whether the identity continues to produce the customer 
 private Boolean flag = true; 
 Maximum number of queues 
 private int maxwaitnum = 0; 
} 

3 Customers and queued queues have, we set up a thread to generate customers, let it constantly produce customers, here is what we said above the time and probability distribution.

/** 
 * @Description: Generating Customer Threads 
 * @Version: 1.1.0 * * 
private class Customerthread extends thread { 
 Private Customerthread (String name) { 
  super (name); 
 } 
 
 @Override public 
 Void Run () {while 
  (flag) { 
   //Team tail adds a new customer 
   if (Math.random () < rate) { 
    Customers.addlast (New Customerbean ()); 
    if (Maxwaitnum < Customers.size ()) { 
     maxwaitnum = Customers.size (); 
    } 
   } 
   int sleeptime = (int) (Math.random () * (maxtime-mintime) + mintime); 
   try { 
    TimeUnit.MILLISECONDS.sleep (sleeptime); 
   } catch (Exception e) { 
    e.printstacktrace (); 
   } 
  } 
 } 
} 

4 If the queue has customers in line to cut the free service desk, you need to get the team head customers to receive services

Public synchronized Customerbean Getcustomerbean () { 
 if (customers = = NULL | | customers.size () < 1) {return 
  null; 
 } 
 return Customers.removefirst (); 
} 

5 customer-related properties and methods are ready, the following set up the service desk-related properties, here we directly set up the help desk as a thread, the definition of some services indicators, such as the number of customer service, total waiting time, total service time, the maximum waiting time.

public class Servantthread extends thread{ 
 //service customer number 
 private static int customernum = 0; 
 Total wait time 
 private static int sumwaittime = 0; 
 Total service time 
 private static int sumservetime = 0; 
 Maximum wait time 
 private static int maxwaittime = 0; 
 Private Boolean flag = false; 
 private String name; 
 

6 The main job of the service desk is to serve the customer, where we write the service-related operations to the thread's Run method.

public void Run () { 
 flag = true; 
 while (flag) { 
  Customerbean customer = Customerquene.getcustomerquene (). Getcustomerbean (); 
  If the customer thread is closed and there are no customers in the queue, the help desk thread closes the release 
  if (customer = null) { 
   if (!) Customerquene.getcustomerquene (). Isflag ()) { 
    flag = false; 
    Print (); 
   } 
   Continue; 
  } 
  Long now = System.currenttimemillis (); 
  int waittime = (int) (Now-customer.getarrivetime ()); 
  Save maximum wait Time 
  if (waittime > Maxwaittime) { 
   maxwaittime = waittime; 
  } 
  The sleep time is the service time of the customer, which represents this period in the service customer 
  try { 
   TimeUnit.MILLISECONDS.sleep (Customer.getservetime ()); 
  } catch ( Exception e) { 
   e.printstacktrace (); 
  } 
  SYSTEM.ERR.PRINTLN (name + "Service Customer time Consuming:" + customer.getservetime () + "Ms\t Customer wait:" + waittime + "MS"); 
  customernum++; 
  Sumwaittime + = waittime; 
  Sumservetime + = Customer.getservetime (); 
   
 } 
 

7 Finally we write a test model to verify the service level

 /** * @Description: * * Package com.lulei.opsearch.quene; 
 
Import Java.util.concurrent.TimeUnit; public class Test {public static void main (string[] args) {//Open Door System.out.println ("Open the door!") 
  "); 
  Boolean flag = true; 
  Customerquene.getcustomerquene (); 
  Long a = System.currenttimemillis (); 
  int servantnum = 10; 
   for (int i = 0; i < Servantnum i++) {servantthread thread = new Servantthread ("Help desk" + i); 
  Thread.Start (); 
   while (flag) {Long b = system.currenttimemillis (); 
    if (B-a > 1 * 1000 && flag) {//closing flag = false; 
    Customerquene.getcustomerquene (). Close (); System.out.println ("The door is closed!") 
   "); 
   SYSTEM.OUT.PRINTLN ("System Running Time:" + (B-A) + "MS"); 
   SYSTEM.OUT.PRINTLN ("System Idle Time:" + ((b-a) * Servantnum-servantthread.getsumservetime ())); 
   Servantthread.print (); 
   try {TimeUnit.SECONDS.sleep (2); 
   catch (Exception e) {e.printstacktrace (); 
 } 
  } 
 } 
 
}

Run results
1) Run Start

2 Customer generated thread shutdown

3 Final Service level

By modifying the number of Help desk, you can evaluate how many help desk should be set up in the current customer situation.

Complete code

1) Customer Class

 /** * @Description: * * Package com.lulei.opsearch.quene; 
 public class Customerbean {//min service time private static int minservetime = 3 * 1000; 
 Maximum service time private static int maxservetime = 15 * 1000; 
 The customer reaches the time private long arrivetime; 
  
 The customer needs the service to be time-consuming private int servetime; 
  Public Customerbean () {//setting arrival time Arrivetime = System.currenttimemillis (); 
 Randomly set customer service time servetime = (int) (Math.random () * (maxservetime-minservetime) + minservetime); 
 public static int Getminservetime () {return minservetime; 
 The public static void Setminservetime (int minservetime) {customerbean.minservetime = Minservetime; 
 public static int Getmaxservetime () {return maxservetime; 
 The public static void Setmaxservetime (int maxservetime) {customerbean.maxservetime = Maxservetime; 
 Public long Getarrivetime () {return arrivetime; 
 } public void Setarrivetime (long arrivetime) {this.arrivetime = Arrivetime; } public int GetserVetime () {return servetime; 
 The public void setservetime (int servetime) {this.servetime = Servetime; 
 } 
}

2) Customer queue

 /** * @Description: * * Package com.lulei.opsearch.quene; 
Import java.util.LinkedList; 
 
Import Java.util.concurrent.TimeUnit; public class Customerquene {//Waiting for customer queue private linkedlist<customerbean> customers = new Linkedlist<customerbea 
 N> (); 
 Next customer. Shortest time private int mintime = 0; 
 Next customer. Maximum time private int maxtime = 1 * 1000; 
 Probability of coming to the customer private double rate = 0.9; 
 Whether the identity continues to produce the customer private Boolean flag = true; 
  
 Maximum number of queues private int maxwaitnum = 0; 
 public int Getmaxwaitnum () {return maxwaitnum; 
 public Boolean Isflag () {return flag; /** * @return * @Author: Lulei * @Description: Get the customer who is queue head/public synchronized Customerbean Getcustome 
  Rbean () {if (customers = null | | | customers.size () < 1) {return null; 
 return Customers.removefirst (); 
  public void Close () {if (flag) {flag = false; }/** * @return * @Author: Lulei * @Description: Get waiting number of customers/public INT Getwaitcustomernum () {return customers.size (); /** * @Description: Generating Customer Threads * @Version: 1.1.0 * * Private class Customerthread extends thread {private Cu 
  Stomerthread (String name) {super (name); @Override public void Run () {while (flag) {//Team tail to add a new customer if (Math.random () < rate) {Cust 
     Omers.addlast (New Customerbean ()); 
     if (Maxwaitnum < Customers.size ()) {maxwaitnum = Customers.size (); 
    an int sleeptime = (int) (Math.random () * (maxtime-mintime) + mintime); 
    try {TimeUnit.MILLISECONDS.sleep (sleeptime); 
    catch (Exception e) {e.printstacktrace (); Start private static class Customerquenedao {private static customerquene Customerquene = NE 
 W Customerquene (); 
  Private Customerquene () {Customerthread customerthread = new Customerthread ("Customer generated thread"); 
 Customerthread.start (); public static Customerquene Getcustomerquene () {REturn Customerquenedao.customerquene; 
 ///single case mode end public int getmintime () {return mintime; 
 The public void setmintime (int mintime) {this.mintime = Mintime; 
 public int Getmaxtime () {return maxtime; 
 The public void setmaxtime (int maxtime) {this.maxtime = MaxTime; 
 Public double getrate () {return rate; 
 The public void setrate (double rate) {this.rate = rate; 
 } 
}

3) Help Desk thread

 /** * @Description: * * Package com.lulei.opsearch.quene; 
 
Import Java.util.concurrent.TimeUnit; 
 
Import Com.lulei.util.ParseUtil; 
 public class Servantthread extends thread{//service customer number private static int customernum = 0; 
 Total wait time private static int sumwaittime = 0; 
 Total service time private static int sumservetime = 0; 
 Maximum wait time private static int maxwaittime = 0; 
 Private Boolean flag = false; 
  
 private String name; 
  Public Servantthread (String name) {super (name); 
 THIS.name = name; 
 public static int Getmaxwaittime () {return maxwaittime; 
 public static int Getsumservetime () {return sumservetime; 
  @Override public void Run () {flag = true; 
   while (flag) {Customerbean customer = Customerquene.getcustomerquene (). Getcustomerbean (); If the customer thread is closed and there are no customers in the queue, the help desk thread closes the release if (customer = null) {if (!) 
     Customerquene.getcustomerquene (). Isflag ()) {flag = false; 
    Print (); 
   } continue; } Long NoW = System.currenttimemillis (); 
   int waittime = (int) (Now-customer.getarrivetime ()); 
   Save maximum wait time if (waittime > Maxwaittime) {maxwaittime = waittime; 
   ///Sleep time is the customer's service time, representing this period of time in the service customer try {TimeUnit.MILLISECONDS.sleep (Customer.getservetime ()); 
   catch (Exception e) {e.printstacktrace (); 
   SYSTEM.ERR.PRINTLN (name + "Service Customer time Consuming:" + customer.getservetime () + "Ms\t Customer wait:" + waittime + "MS"); 
   customernum++; 
   Sumwaittime + = waittime; 
    
  Sumservetime + + customer.getservetime (); The public static void print () {if (Customernum > 0) {System.out.println ("----------------------------- 
   ---------"); 
   SYSTEM.OUT.PRINTLN ("Service customer Number:" + customernum); 
   System.out.println ("Max wait Time:" + maxwaittime); 
   System.out.println ("Waiting number of Customers:" + customerquene.getcustomerquene (). Getwaitcustomernum ()); 
   SYSTEM.OUT.PRINTLN ("Maximum number of customers waiting:" + customerquene.getcustomerquene (). Getmaxwaitnum ()); Output the average waiting time of the customer, leaving two decimal System.out.println ("Average customer waiting time:" + parseutil.parsedoubletodouble ((Sumwaittime * 1.0/customernum), 2) + "MS"); 
   System.out.println ("Average customer service time:" + parseutil.parsedoubletodouble (Sumservetime * 1.0/customernum), 2) + "MS"); 
  SYSTEM.OUT.PRINTLN ("Total system service Time:" + sumservetime + "MS"); 
 } 
 } 
}

4) test model

 /** * @Description: * * Package com.lulei.opsearch.quene; 
 
Import Java.util.concurrent.TimeUnit; public class Test {public static void main (string[] args) {//Open Door System.out.println ("Open the door!") 
  "); 
  Boolean flag = true; 
  Customerquene.getcustomerquene (); 
  Long a = System.currenttimemillis (); 
  int servantnum = 10; 
   for (int i = 0; i < Servantnum i++) {servantthread thread = new Servantthread ("Help desk" + i); 
  Thread.Start (); 
   while (flag) {Long b = system.currenttimemillis (); 
    if (B-a > 1 * 1000 && flag) {//closing flag = false; 
    Customerquene.getcustomerquene (). Close (); System.out.println ("The door is closed!") 
   "); 
   SYSTEM.OUT.PRINTLN ("System Running Time:" + (B-A) + "MS"); 
   SYSTEM.OUT.PRINTLN ("System Idle Time:" + ((b-a) * Servantnum-servantthread.getsumservetime ())); 
   Servantthread.print (); 
   try {TimeUnit.SECONDS.sleep (2); 
   catch (Exception e) {e.printstacktrace ();  } 
  } 
 } 
 
}

The above is about the Java implementation of the principle of queuing theory in detail, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.