This is the 13th day of my participation in the August More Text Challenge.More challenges in August

1. Functional classification

Producer-customer service log in to the system consumer-customer customer serviceCopy the code

2. Simple processes

1. When there is no effective customer service, wait for customer service to connect to the system. 2. Customer consumption customer service, available customer service -1 here is a random query of an available customer service can also write some other strategies such as obtaining the first available customer service maintenance service statistics according to the minimum service quantity of 4. When the communication is completed, customer service +1 can be used. 5. After the customer service exits, customer service -1 can be usedCopy the code

3. The implementation

We can use the Semaphore in customer service to judge the customer consumption customer service is threaded customer service operation CopyOnWriteArrayListCopy the code

3.1 Simple entity definition of customer service

public class Customer { private String name; // name private String code; }}Copy the code

3.2 Realization of customer service management

Public class CustomerManagerList {public static Semaphore workSemaphore=new Semaphore(0); Private static List<Customer> cusSet= new CopyOnWriteArrayList<Customer>(); Private static List<Customer> cusWorkSet= new CopyOnWriteArrayList<Customer>(); private static List<Customer> cusWorkSet= new CopyOnWriteArrayList<Customer>(); Public static void addCustomer(Customer c) {cusset.add (c); System.out.println(c.tostring ()+" Access..." ); workSemaphore.release(); Public static void addWork(Customer c) throws InterruptedException {cusworkset.add (c); } public static void removeWork(Customer c) {cusworkset.remove (c); workSemaphore.release(); } // Exit public static void removeCustomer(Customer c) throws InterruptedException {cusset.remove (c); cusWorkSet.remove(c); workSemaphore.tryAcquire(); Public static Customer getWorkRandom() throws InterruptedException {worksemaphore.acquire (); List<Customer> cusNotWork=new CopyOnWriteArrayList<Customer>(); cusNotWork.addAll(cusSet); cusNotWork.removeAll(cusWorkSet); if(0==cusNotWork.size()) { workSemaphore.release(); // Return null without data consumption; }else { Customer result=cusNotWork.get(new Random().nextInt(cusNotWork.size())); addWork(result); // Add to work return result; }}}Copy the code

3.3 Realization of customer service consumption

Public class CustomerRun implements Runnable{@override public void run() {try { System.out.println(" waiting...") ); System.out.println(CustomerManager.workSemaphore.availablePermits()); Customer c = CustomerManagerList.getWorkRandom(); if (null == c) { return; } system.out.println (c.tostring () + "in communication..." ); Thread.sleep(5 * 1000); System.out.println(c.tostring () + "End of communication..." ); CustomerManagerList.removeWork(c); } catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code

3.4 test

Public static void test2() {for (int I = 0; i < 100; i++) { CustomerRun cr = new CustomerRun(); Thread t = new Thread(cr); t.start(); } for (int I = 0; i < 15; i++) { CustomerManagerList.addCustomer(new Customer("name" + i, "code" + i)); } for (int I = 15; i < 20; i++) { CustomerManagerList.addCustomer(new Customer("name" + i, "code" + i)); }}Copy the code

3.5 the effect