- package controller.com.tanhua.controller.domain;
-
- import org.junit.Test;
-
- import java.util.UUID;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.SynchronousQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
-
- /**
- * Created by Lenovo on 2023/10/10.
- */
- public class MainEntity {
- public static void main(String[] args) {
-
- }
-
- /**
- * 假设有一个银行网点,有多个窗口用于处理客户的业务。
- * 现在需要编写一个程序模拟该网点的业务处理过程,要求如下: 网点有 N 个窗口,用一个线程池管理这些窗口的线程。
- * 每个窗口可以同时处理多个客户的业务,但同时最多只能服务 K 个客户。
- * 客户可以在任意窗口进行业务办理,但需要等待窗口空闲后才能进行服务。
- * 当一个客户到达银行网点后,随机选择一个窗口进行排队等待服务,直到业务处理完成。
- * 在业务处理过程中,客户不能在多个窗口进行业务办理。
- * 请根据以上要求,编写一个 Java 程序实现银行网点的业务处理过程。
- *
- * 要求: 1、java代码实现 2、构造3组测试用例,可以执行通过 3、注释、日志齐全
- */
- @Test
- public void testBankBusiness(){
-
- System.out.println("hello test!");
- /**
- * N 个窗口 10个用户线程日志就绪运行
- */
- for (int i = 0; i < 10; i++) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- Business business = new Business();
- String s = UUID.randomUUID().toString();
- business.setId(s);
- Customer customer = new Customer();
- String s1 = UUID.randomUUID().toString();
- customer.setId(s1);
- business.setName("用户"+customer.getId()+" 的银行业务id"+business.getId()+"办理完成!");
-
- System.out.println(business.getName());
- }
- }).start();
- }
- }
-
- @Test
- public void testBankBusinessThreadPool(){
- System.out.println("hello test");
- ExecutorService executorService = newCachedThreadPool();
- executorService.execute(new Runnable() {
- @Override
- public void run() {
- for (int i = 0; i < 10; i++) {
- Business business = new Business();
- String s = UUID.randomUUID().toString();
- business.setId(s);
- Customer customer = new Customer();
- String s1 = UUID.randomUUID().toString();
- customer.setId(s1);
- business.setName("用户"+customer.getId()+" 的银行业务id"+business.getId()+"办理完成!");
-
- System.out.println(business.getName());
- }
- }
- });
- }
-
- //创建CachedThreadPool
- public ExecutorService newCachedThreadPool() {
- return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
- 60L, TimeUnit.SECONDS,
- new SynchronousQueue
()); - }
- }
-
- package controller.com.tanhua.controller.domain;
-
- /**
- * Created by Lenovo on 2023/10/10.
- */
- public class Bank {
- private String id;
- private String name;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
- package controller.com.tanhua.controller.domain;
-
- /**
- * Created by Lenovo on 2023/10/10.
- */
- public class BankWindows {
- }
- package controller.com.tanhua.controller.domain;
-
- /**
- * Created by Lenovo on 2023/10/10.
- */
- public class Business {
-
- private String id;
- private String name;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Business{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- '}';
- }
- }
- package controller.com.tanhua.controller.domain;
-
- /**
- * Created by Lenovo on 2023/10/10.
- */
- public class Customer {
-
- private String id;
- private String name;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }