Multi-threaded Combat (II): Bank Business Scheduling system

Source: Internet
Author: User

I. Project requirements:

1. There are 6 business windows in the Bank, 1-4 window is normal window, window 5th is Quick window, window 6th is VIP window.
2. There are three types of customers: VIP customers, ordinary customers, fast customers (for example, pay utilities, such as electricity charges, such as business customers).
3. Asynchronous random generation of various types of customers, the probability ratio of generating various types of users is: VIP Customers: Ordinary customers: Fast customer = 1:6: 3.
4. The time required for the customer to transact the business is the maximum and minimum, within which the time required for each VIP customer and the ordinary customer to transact the business is randomly set, and the time required for the fast customer to transact the business is the minimum (hint: the process of processing the business can be simulated by thread sleep).
5. Each type of customer in their corresponding window in order to transact business sequentially. When the VIP (6th) window and the Fast Business (5th) window do not have customers waiting for business, these two windows can handle the business of ordinary customers, and once the corresponding customers waiting for the business, the priority is to deal with the customer's business.
6. Randomly generated customer time interval and business processing time maximum and minimum value custom, can be set.
7. Do not require the implementation of the GUI, only consider the system logic implementation, log mode can show the results of the program operation.


Two. Object-oriented Analysis

There are three types of customers: VIP customers, ordinary customers, fast customers, asynchronous random generation of various types of customers, all types of customers in their corresponding window in order to transact business sequentially. Each customer is in fact a number of the bank's machine to generate numbers to represent the way. So, I think of having a number manager object, so that the object constantly generate numbers, it is equal to randomly generated the customer.


Because there are three types of customers, each type of customer's numbering is completely independent, so, the system to generate a total of three number manager object, each management of a class of users queue number. These three number manager objects are managed by a number machine, and this number machine can only have one in the whole system, so it should be designed as a singleton.


Various types of customers in their corresponding window in order to transact business sequentially, should be accurate, should be the window in turn call. How does each window know which number to call? It must be asking for the corresponding number manager, which is the service window each time the number manager gets the number that is currently being serviced.


Three. Code implementation


1. Numbermanager:

public class Numbermanager {private int newnumber = 0;private list<integer> numlist = new Arraylist<integer> () ;p ublic synchronized Integer generatenewnumber () {numlist.add (++newnumber); return newnumber;} Public synchronized Integer Fetchnumber () {if (numlist.size () > 0) {return (Integer) numlist.remove (0);} else {return n ull;}}}
defines a member variable to store the previous customer number and a collection of queues to store all the customer numbers waiting for the service.

Define a method for generating a new number and get the number to be serviced immediately, and the two methods are manipulated by different threads for the same data, so synchronize.


2. Numbermachine:

public class Numbermachine {private Numbermachine () {}private static numbermachine instance = new Numbermachine ();p ublic s Tatic Numbermachine getinstance () {return instance;} Private Numbermanager Commonmanager = new Numbermanager ();p rivate numbermanager expressmanager = new Numbermanager (); Private Numbermanager Vipmanager = new Numbermanager ();p ublic Numbermanager Getcommonmanager () {return commonmanager;} Public Numbermanager Getexpressmanager () {return expressmanager;} Public Numbermanager Getvipmanager () {return vipmanager;}}
define three member variables to point to three Numbermanager objects, respectively, the number manager for normal, fast, and VIP customers, and define three corresponding methods to return the three Numbermanager objects. Design the Numbermachine class into a single case.


3. CustomerType:

public enum CustomerType {COMMON, EXPRESS, vip;public string toString () {String name = Null;switch (this) {case Common:nam E = "normal"; break;case Express:name = "Fast"; break;case Vip:name = name (); break;} return name;}}
There are three types of customers in the system, so define an enumeration class that defines three members representing three types of customers, respectively.
Overrides the ToString method, which returns the Chinese name of the type. This is reconstructed at the back of the code and is not considered at first.


4. Servicewindow:

public class Servicewindow {private CustomerType type = customertype.common;private int number = 1;public CustomerType get Type () {return type;} public void SetType (CustomerType type) {this.type = type;} public void Setnumber (int number) {this.number = number;} public void Start () {Executors.newsinglethreadexecutor (). Execute (new Runnable () {public void run () {while (true) {try{ Switch (type) {case Common:commonservice (), Break;case express:expressservice (); break;case Vip:vipservice (); break;}} catch (Exception e) {e.printstacktrace ();}}});} private void Commonservice () throws Exception {String windowname = "First" + This.number + "number" + This.type + "window"; System.out.println (Windowname + "start getting common tasks!"); Nteger Servicenumber = Numbermachine.getinstance (). Getcommonmanager (). Fetchnumber (); if (servicenumber! = null) { System.out.println (Windowname + "Start for the first" + Servicenumber + "number of ordinary customer service"); int maxrandom = Constants.max_service_time-constant S.min_service_time;int serviceTime = new Random (). Nextint (Maxrandom) + 1 + Constants.min_service_time; Thread.Sleep (ServiceTime); System.out.println (Windowname + "completed for the first" + Servicenumber + "number of ordinary customer service, total time-consuming" + servicetime/1000 + "seconds");} else {System.out.println (windowname + "not taken to normal task, idle for one second"); Thread.Sleep (1000);}} private void Expressservice () throws Exception {Integer servicenumber = Numbermachine.getinstance (). Getexpressmanager ( ). Fetchnumber (); String windowname = "First" + This.number + "number" + This.type + "window"; System.out.println (Windowname + "start getting quick quests!"); if (servicenumber! = null) {System.out.println (Windowname + "starts with" + Servicenumber + "fast customer service"); int serviceTime = Constant S.min_service_time; Thread.Sleep (ServiceTime); System.out.println (Windowname + "completed for the first" + Servicenumber + "Number of fast customer service, total time-consuming" + servicetime/1000 + "seconds");} else {System.out.println (windowname +) did not take the quick task! "); Commonservice ();}} private void Vipservice () throws Exception {Integer servicenumber = Numbermachine.getinstance (). Getvipmanager (). Fetchnumber (); String windowname = "First" + This.number + "number" + This.type + "windowMouth "; System.out.println (Windowname + "start getting VIP quests!"); if (servicenumber! = null) {System.out.println (Windowname + "starts with VIP customer service" + Servicenumber + "); int maxrandom = Constants . Max_service_time-constants.min_service_time;int serviceTime = new Random (). Nextint (Maxrandom) + 1 + constants.min_ Service_time; Thread.Sleep (ServiceTime); System.out.println (Windowname + "completed for the first" + Servicenumber + "VIP customer service, total time-consuming" + servicetime/1000 + "seconds");} else {System.out.println (windowname +) did not take the VIP task! "); Commonservice ();}}}
Defines a start method that internally initiates a thread that loops through three different methods, respectively, according to the category of the service window.
Three methods are defined to serve three kinds of customers, and in order to observe the effect of operation, detailed information should be printed out in detail.


5. Constants

public class Constants {public static int max_service_time = 10000;//10 seconds! public static int min_service_time = 1000; 1 seconds! public static int common_customer_interval_time = 1; }

6. MainClass:

public class MainClass {public static void main (string[] args) {//Generate 4 normal windows (open 4 threads) for (int i = 1; I <= 4; i++) {Servic Ewindow Commonwindow = new Servicewindow (); Commonwindow.setnumber (i); Commonwindow.start (); }//generates 1 quick windows (opens a thread) Servicewindow Expresswindow = new Servicewindow (); Expresswindow.settype (customertype.express); Expresswindow.start ();//Generate 1 VIP windows (open a thread) Servicewindow Vipwindow = new Servicewindow (); Vipwindow.settype ( CUSTOMERTYPE.VIP); Vipwindow.start ()//Every second there is a regular customer pick (turn on timer) Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {public void run () {Integer servicenumber = Numbermachine.getinstance (). Getcommonmanager (). Generatenewnumber (); System.out.println ("the" + Servicenumber + "number of ordinary customers are waiting for service! ");}}, 0, Constants.common_customer_interval_time, timeunit.seconds);//Every two seconds there is a fast customer number (turn on timer) Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {public void run () {Integer Servicenumber = Numbermachine.getinstance (). Getexpressmanager (). GeneratenewnumbeR (); System.out.println ("No." + Servicenumber + "Fast customer is waiting for service! ");}}, 0, Constants.common_customer_interval_time * 2, timeunit.seconds);//Every six seconds there is a VIP customer to take the number (turn on the timer) Executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {public void run () {Integer Servicenumber = Numbermachine.getinstance (). Getvipmanager (). Generatenewnumber (); System.out.println ("No." + Servicenumber + "VIP customer is waiting for service!") ");}}, 0, Constants.common_customer_interval_time * 6, timeunit.seconds);}}
Create 4 normal windows with a for loop, then create 1 quick windows and a VIP window.
Then create three timers to create a new regular customer number, a new fast customer number, a new VIP customer number, respectively.


Multi-threaded Combat (II): Bank Business Scheduling system

Related Article

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.