紹介された:
しばらく前に、私はビジネスを処理するために銀行に行きましたが、並んでいる人がたくさんいました。自分でビジネスを処理するのに5分もかかりませんでしたが、私は2時間待ちました(多くの人がこの状況に遭遇したと思います)。私はこのサービスレベルについて言葉を失いましたが、問題は再び発生します。銀行は、リソースの全体的なサービス品質と利用率を確保するために、いくつかのウィンドウを開く必要がありますか?次に、キュー理論を通じてこの問題をシミュレートします。
キュー理論の紹介
キューイング理論は、システムのランダムな収集と分散の現象、およびランダムシステムの作業工学を研究する数学的理論と方法です。ランダムサービスシステムの理論としても知られており、運用研究の分野です。以下のキューイング理論を簡素化し、最初に次の図を見てみましょう。
写真の左側にいくつかの青いサービスデスクを配置し、右側にやってくる赤い顧客と、真ん中に黄色の待合室があります。サービスデスクがアイドル状態である場合、顧客はサービスを直接受信できます。そうしないと、黄色のエリアで待つ必要があります。カスタマーサービスの順序は、最初のカムと現在のサービスの原則を採用しています。顧客の確率分布が来ることがわかっている場合、より良いサービスレベルを達成し、サービスデスクの使用率を確保するために、左側にいくつかのサービスデスクを配置するにはどうすればよいですか?次に、この問題をシミュレートするためのモデルを構築します。
キューイング理論は段階的に実装されます
1)キューイング理論については、まず顧客の属性を決定し、顧客がいつ到着するか、必要なサービス時間などを知る必要があります。最初に顧客クラスを作成し、顧客サービスの最大時間と最小時間を指定します。ここでは、単純化するために、サービス時間は完全にランダムであると直接考えます。
Public Class CustomerBean {//最小サービス時間Private Static Int Minservetime = 3 * 1000; //最大サービス時間private static int maxservetime = 15 * 1000; //顧客はプライベートの長い到着に到達します。 //顧客はサービス時間プライベートint servetime; public customerbean(){//到着時間到着時間= System.CurrentTimemillis(); //ランダムセットカスタマーサービス時間servetime =(int)(math.random() *(maxservetime -minservetime) + minservetime); }} 2)上記の顧客を定義し、キューキューを定義する必要があります。まず、キューの属性を見てみましょう。ここでは、キューに登録された顧客を保存する配列を定義し、次の顧客が到着する可能性と顧客が来る可能性を定義します(次の顧客の間隔が3の場合、ここで簡単に説明する必要がありますが、確率と満足して計算されますが、顧客はQueueに入りません。
Public Class CustomerQuene {//顧客キューを待っているPrivate LinkedList <CustomerBean>顧客= new LinkedList <CustomerBean>(); //次の顧客がプライベートになるための最短時間int mintime = 0; //次の顧客がプライベートになるための最大時間int maxtime = 1 * 1000; //顧客が来る可能性はプライベートダブルレート= 0.9; //顧客のプライベートブールフラグ= trueかどうかを特定します。 // Private int maxwaitnum = 0をキューイングする人の最大数。 } 3)顧客とキューがある場合、顧客を生成するためのスレッドを設定して、顧客を継続的に生成します。上記の時間と確率分布は次のとおりです。
/ ***@説明:顧客スレッドを生成*@バージョン:1.1.0*/ private class customerthread extends thread {private customerthread(string name){super(name); } @Override public void run(){while(flag){//チームの終わりに新しい顧客を追加するif(math.random()<reate){customers.addlast(new CustomerBean()); if(maxwaitnum <customers.size()){maxwaitnum = customers.size(); }} int sleeptime =(int)(math.random() *(maxtime -mintime) + mintime); {timeunit.milliseconds.sleep(sleeptime); } catch(Exception e){e.printstacktrace(); }}}}} 4)キューにキューイングに顧客がいる場合は、無料のサービスデスクでカットするために、顧客にチームのヘッドにサービスを受け取る必要があります。
Public Synchronized CustomerBean getCustomerBean(){if(customers == null || customers.size()<1){return null; } customers.removeFirst(); } 5)顧客関連の属性と方法はすべて準備ができています。サービスデスク関連の属性を設定しましょう。ここでは、サービスデスクをスレッドに直接設定して、提供される顧客の数、合計待機時間、総サービス時間、最大待機時間など、いくつかのサービスインジケーターを定義します。
public class servantthreadはスレッドを拡張します{//サービス顧客の数private static int customernum = 0; //総待機時間private private static int sumwaittime = 0; //総サービス時間プライベート静的int sumservetime = 0; //最大待機時間private static int maxwaittime = 0;プライベートブールフラグ= false;プライベート文字列名; } 6)サービスデスクの主な仕事は、顧客にサービスを提供することです。ここでは、スレッドの実行方法に顧客にサービスを提供することに関連する操作を書きます。
public void run(){flag = true; while(flag){customerbean customer = customerquene.getCustomerquene()。getCustomerbean(); //顧客スレッドが閉じられており、キューに顧客がいない場合、サービスデスクスレッドが閉じてリリースされますif(customer == null){if(!customerquene.getCustomerquene()。isflag()){flag = false; print(); } 続く; } long now = system.currenttimemillis(); int waittime =(int)(now -customer.getarrivetime()); //最大待機時間を保存する場合(waittime> maxwaittime){maxwaittime = waittime; } //睡眠時間は顧客のサービス時間であり、顧客にサービスを提供するこの期間中のサービス期間を表します。 try {timeunit.milliseconds.sleep(customer.getServetime()); } catch(Exception e){e.printstacktrace(); } system.err.println(name + "顧客にサービスを提供する時間:" + customer.getServeservetime() + "MS/T Customer waiting:" + waittime + "ms"); customernum ++; sumwaittime += waittime; sumservetime += customer.getServetime(); }} 7)最後に、サービスレベルを確認するためのテストモデルを書きます
/ ** *@説明: */パッケージcom.lulei.opsearch.quene; java.util.concurrent.timeunitをインポートします。 public class test {public static void main(string [] args){//ドアシステムを開きます。ブールフラグ= true; customerquene.getCustomerquene(); long a = system.currenttimemillis(); int servicenum = 10; for(int i = 0; i <servicenum; i ++){servantthread thread = new servantthread( "service desk"+i); thread.start(); } while(flag){long b = system.currenttimemillis(); if(b -a> 1 * 60 * 1000 && flag){// close flag = false; customerquene.getCustomerquene()。close(); system.out.println(「ドアを閉じて顧客を迎えないでください!」); } system.out.println( "システムの実行時間:" +(b -a) + "ms"); system.out.println( "システムアイドル時間:" +((b -a) * servantnum -servantthread.getSumservetime())); servantthread.print(); {timeunit.seconds.sleep(2)を試してください。 } catch(Exception e){e.printstacktrace(); }}}}実行結果
1)操作の開始
2)顧客はスレッドクロージングを生成します
3)最終サービスレベル
サービスデスクの数を変更することにより、現在の顧客の状況でセットアップする必要があるサービスデスクの数を評価できます。
完全なコード
1)顧客カテゴリ
/ ** *@説明: */パッケージcom.lulei.opsearch.quene; Public Class CustomerBean {//最小サービス時間Private Static Int Minservetime = 3 * 1000; //最大サービス時間private static int maxservetime = 15 * 1000; //顧客はプライベートの長い到着に到達します。 //顧客はサービス時間プライベートint servetime; public customerbean(){//到着時刻arrivetime = system.currenttimemillis(); //カスタマーサービス時間servetime =(int)(math.random() *(maxservetime -minservetime) + minservetime); } public static int getMinservetime(){return minservetime; } public static void setminservetime(int minservetime){customerbean.minservetime = minservetime; } public static int getMaxServetime(){return maxServetime; } 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; } public void setServetime(int servetime){this.servetime = servetime; }}2)顧客キュー
/ ** *@説明: */パッケージcom.lulei.opsearch.quene; java.util.linkedListをインポートします。 java.util.concurrent.timeunitをインポートします。 Public Class CustomerQuene {//顧客キューを待っているPrivate LinkedList <CustomerBean>顧客= new LinkedList <CustomerBean>(); //次の顧客がプライベートになるための最短時間int mintime = 0; //次の顧客がプライベートになるための最大時間int maxtime = 1 * 1000; //顧客プライベートダブルレート= 0.9になる確率。 //顧客がプライベートブールフラグを生成し続けているかどうかを特定します= true; //最大数の人々はプライベートint maxwaitnum = 0; public int getMaxWaitnum(){return maxwaitnum; } public boolean isflag(){return flag; } / ** * @return * @author:lulei * @description:顧客をキューのヘッドに乗せてください * / public同期Customerbean getCustomerbean(){if(customer == null || customers.size()<1){return null; } customers.removeFirst(); } public void close(){if(flag){flag = false; }} / ** * @return * @author:lulei * @description:待機中の顧客の数を取得 * / public int getWaitCustomernum(){return customers.size(); } / ***@説明:顧客スレッドを生成*@バージョン:1.1.0* / private class customerthread extends thread {private customerthread(string name){super(name); } @Override public void run(){while(flag){//チームの終わりに新しい顧客を追加するif(math.random()<reate){customers.addlast(new CustomerBean()); if(maxwaitnum <customers.size()){maxwaitnum = customers.size(); }} int sleeptime =(int)(math.random() *(maxtime -mintime) + mintime); {timeunit.milliseconds.sleep(sleeptime); } catch(Exception e){e.printstacktrace(); }}}}}} // Singleton Mode Private Static Class CustomerQuenedaoを開始{private static CustomerQuene customerquene = new CustomerQuene(); } private customerquene(){customerthread customerthread = new CustomerThread( "Customer Generationスレッド"); customerthread.start(); } public static CustomerQuene getCustomerquene(){customerquenedao.customerqueneを返します。 } // singletonモードEnd public int getMintime(){return mintime; } public void setMintime(int mintime){this.mintime = mintime; } public int getMaxtime(){return maxtime; } public void setmaxtime(int maxtime){this.maxtime = maxtime; } public double getrate(){return rate; } public void setrate(double rate){this.rate = reat; }} 3)サービスデスクスレッド
/ ** *@説明: */パッケージcom.lulei.opsearch.quene; java.util.concurrent.timeunitをインポートします。 com.lulei.util.parseutilをインポートします。 public class servantthreadはスレッドを拡張します{//サービス顧客の数private static int customernum = 0; //総待機時間private private static int sumwaittime = 0; //総サービス時間プライベート静的int sumservetime = 0; //最大待機時間private static int maxwaittime = 0;プライベートブールフラグ= false;プライベート文字列名; 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(customer == null){if(!customerquene.getCustomerquene()。isflag()){flag = false; print(); } 続く; } long now = system.currenttimemillis(); int waittime =(int)(now -customer.getarrivetime()); //最大待機時間を保存する場合(waittime> maxwaittime){maxwaittime = waittime; } //スリープ時間は顧客のサービス時間であり、顧客のtry {timeunit.milliseconds.sleep(customer.getServetime())を表します。 } catch(Exception e){e.printstacktrace(); } system.err.println(name + "顧客にサービスを提供する時間:" + customer.getServetime() + "ms/t顧客待機:" + waittime + "ms"); customernum ++; sumwaittime += waittime; sumservetime += customer.getServetime(); }} public static void print(){if(customernum> 0){ System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //顧客の平均待機時間を出力し、2つの小数の場所システムを保持します。 Customernum)、2) + "MS"); 4)モデルをテストします
/ ** *@説明: */パッケージcom.lulei.opsearch.quene; java.util.concurrent.timeunitをインポートします。 public class test {public static void main(string [] args){//ドアシステムを開きます。ブールフラグ= true; customerquene.getCustomerquene(); long a = system.currenttimemillis(); int servicenum = 10; for(int i = 0; i <servicenum; i ++){servantthread thread = new servantthread( "service desk"+i); thread.start(); } while(flag){long b = system.currenttimemillis(); if(b -a> 1 * 60 * 1000 && flag){// close flag = false; customerquene.getCustomerquene()。close(); system.out.println(「ドアを閉じて顧客を迎えないでください!」); } system.out.println( "システムの実行時間:" +(b -a) + "ms"); system.out.println( "システムアイドル時間:" +((b -a) * servantnum -servantthread.getSumservetime())); servantthread.print(); {timeunit.seconds.sleep(2)を試してください。 } catch(Exception e){e.printstacktrace(); }}}}上記は、Javaがキューイング理論を実装する原則の詳細な紹介です。みんなの学習に役立つことを願っています。