Multithreading-bank cashier Problems

Source: Internet
Author: User

Multithreading-bank cashier Problems

The questions are as follows:

 

/* Simulate the logic of the banking business scheduling system. The specific requirements are as follows: there are 6 business windows in the bank, 1-4 are normal windows, and 5 are quick windows, window 6 is the VIP window. There are three types of customers: VIP customers, ordinary customers, and fast customers (customers who handle services such as water, electricity, and telephone fees ). Asynchronous random generation of various types of customers, the probability ratio of each type of user generation is: VIP customers: ordinary customers: Fast customers = 1: 6: 3. The maximum and minimum time required for the customer to process the business is set. In this range, the time required for each VIP customer and ordinary customer to process the business is set to the minimum time required for the quick customer to process the business (tip: the business process can be simulated by thread Sleep ). Each type of customer handles the business in sequence in its corresponding window. When the VIP (6) window and quick business (5) window do not have customers waiting for business processing, these two windows can process the business of ordinary customers, once a corresponding customer is waiting for processing the business, the corresponding customer's business is prioritized. You can set the time interval of randomly generated customers and the maximum and minimum values of business processing time. GUI is not required. You only need to implement the system logic. You can print the program running result. */

The solution of this problem draws on the section on the bank cashier simulation in the Java programming ideology. It mainly uses the thread-Safe Container ArrayBlockingQueue directly. The Code is as follows:

 

Customer. java this source file defines three types of customers and the enumerated types of customers.

 

package banktellersimulation;enum CUSTOMER{VIP, //1ORDINARY, //6QUICK  //3}public class Customer {private final CUSTOMER id;private final int servicetime;public Customer(CUSTOMER customer){if (customer == CUSTOMER.VIP){servicetime = 500;} else if (customer == CUSTOMER.ORDINARY){servicetime = 300;} else {servicetime = 100;} id = customer;}public int getServiceTime(){return servicetime;}public String toString(){return id.toString() + : + getServiceTime();}}
CustomerGenerator. java is a producer model that adds a customer-consumer to the corresponding container.

 

 

package banktellersimulation;import java.util.ArrayList;import java.util.Random;import java.util.concurrent.ArrayBlockingQueue;import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;public class CustomerGenerator implements Runnable{private ArrayBlockingQueue
 
   customerlinevip;private ArrayBlockingQueue
  
    customerlineoridinary;private ArrayBlockingQueue
   
     customerlinequick;private Random rand = new Random();private double[] data = new double[]{0.0, 0.1, 0.7, 1.0};public CustomerGenerator(ArrayBlockingQueue
    
      customerlinevip,ArrayBlockingQueue
     
       customerlineoridinary,ArrayBlockingQueue
      
        customerlinequick) {super();this.customerlinevip = customerlinevip;this.customerlineoridinary = customerlineoridinary;this.customerlinequick = customerlinequick;}@Overridepublic void run() {while(!Thread.interrupted()){int i = 0;double temp = rand.nextDouble();for (i = 0; i < data.length - 1; i++){if (temp >= data[i]&& temp < data[i + 1]){break;}}switch (CUSTOMER.values()[i]){case VIP:try {customerlinevip.put(new Customer(CUSTOMER.VIP));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;case ORDINARY:try {customerlineoridinary.put(new Customer(CUSTOMER.ORDINARY));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;case QUICK:try {customerlinequick.put(new Customer(CUSTOMER.QUICK));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;default:}}}}
      
     
    
   
  
 

Window. java simulated Bank Window class

 

 

package banktellersimulation;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.TimeUnit;public class Window implements Runnable{private final int id;private ArrayBlockingQueue
 
   customerlinevip;private ArrayBlockingQueue
  
    customerlineoridinary;private ArrayBlockingQueue
   
     customerlinequick;public Window(int id, ArrayBlockingQueue
    
      customerlinevip,ArrayBlockingQueue
     
       customerlineoridinary,ArrayBlockingQueue
      
        customerlinequick){this.customerlinevip = customerlinevip;this.customerlineoridinary = customerlineoridinary;this.customerlinequick = customerlinequick;this.id = id;}@Overridepublic void run() {try{while (!Thread.interrupted()){Customer temp = null;if (id >= 1 && id <= 4){temp = customerlineoridinary.take();TimeUnit.MICROSECONDS.sleep(temp.getServiceTime());} else if (id == 5){if (customerlinequick.size() == 0 && customerlineoridinary.size() != 0){temp = customerlineoridinary.take();TimeUnit.MICROSECONDS.sleep(temp.getServiceTime());} else {temp = customerlinequick.take();TimeUnit.MICROSECONDS.sleep(temp.getServiceTime());}} else if (id == 6){if (customerlinevip.size() == 0 && customerlineoridinary.size() != 0){temp = customerlineoridinary.take();TimeUnit.MICROSECONDS.sleep(temp.getServiceTime());} else {temp = customerlinevip.take();TimeUnit.MICROSECONDS.sleep(temp.getServiceTime());}}System.out.println(temp + deal by: + this);}}catch (InterruptedException e) {e.printStackTrace();}}public String toString(){return id + ;}}
      
     
    
   
  
 

Bank. java test class, main function run by the thread

 

 

package banktellersimulation;import java.util.concurrent.ArrayBlockingQueue;public class Bank {public static void main(String[] args){ArrayBlockingQueue
 
   customerlinevip = new ArrayBlockingQueue
  
   (20);ArrayBlockingQueue
   
     customerlineoridinary = new ArrayBlockingQueue
    
     (20);ArrayBlockingQueue
     
       customerlinequick = new ArrayBlockingQueue
      
       (20);new Thread(new CustomerGenerator(customerlinevip, customerlineoridinary, customerlinequick)).start();for (int i = 1; i <=6 ;i++){new Thread(new Window(i, customerlinevip, customerlineoridinary, customerlinequick)).start();}}}
      
     
    
   
  
 
 

 

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.