代码修订与功能更新:

- 新增策略模式示例:在设计模式包中引入策略模式(StrategyPattern),实现支付策略的灵活选择与执行。
- 单例模式示例更新:优化购物车示例,引入线程安全的单例模式实现,并调整类定义与实例化过程。
- 处理线程池执行异常:在ThreadDemo1中处理线程池执行时的异常情况,确保任务异常(如除零错误)时程序的稳定运行。
- 另外,修正了一些代码中的笔误并进行了格式化处理,以保持代码的清晰与规范。
This commit is contained in:
kyriewhluo 2024-08-23 18:26:39 +08:00
parent 8f722ebb0c
commit ffa1d4435b
4 changed files with 423 additions and 55 deletions

View File

@ -1,64 +1,16 @@
package cn.whaifree.designPattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.function.BiConsumer;
public class SingletonPattern {
/**
* https://kamacoder.com/problempage.php?pid=1074
*
* 设计模式专题之单例模式1.小明的购物车
* 题目描述
* 小明去了一家大型商场拿到了一个购物车并开始购物请你设计一个购物车管理器记录商品添加到购物车的信息商品名称和购买数量并在购买结束后打印出商品清单在整个购物过程中小明只有一个购物车实例存在
* 输入描述
* 输入包含若干行每行包含两部分信息分别是商品名称和购买数量商品名称和购买数量之间用空格隔开
* 输出描述
* 输出包含小明购物车中的所有商品及其购买数量每行输出一种商品的信息格式为 "商品名称 购买数量"
* 输入示例
* Apple 3
* Banana 2
* Orange 5
* 输出示例
* Apple 3
* Banana 2
* Orange 5
* 提示信息
* 本道题目请使用单例设计模式
*
* 使用私有静态变量来保存购物车实例
*
* 使用私有构造函数防止外部直接实例化
*/
}
static class ManagerCar{
private HashMap<String, Integer> carHas = null;
public ManagerCar() {
this.carHas = new HashMap<>();
}
public HashMap<String, Integer> getCarHas() {
return carHas;
}
private volatile static ManagerCar managerCar;
public static ManagerCar getManagerCar() {
if (managerCar == null) {
synchronized (ManagerCar.class) {
if (managerCar == null) {
managerCar = new ManagerCar();
}
}
}
return managerCar;
}
}
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
HashMap<String, Integer> carHas = ManagerCar.getManagerCar().getCarHas();
@ -66,7 +18,7 @@ public class SingletonPattern {
while (true) {
String nextLine = scanner.nextLine();
if ("stop".equals(nextLine)) {
if ("exit".equals(nextLine)) {
break;
}
String[] s = nextLine.split(" ");
@ -88,6 +40,121 @@ public class SingletonPattern {
scanner.close();
}
}
/**
* https://kamacoder.com/problempage.php?pid=1074
*
* 设计模式专题之单例模式1.小明的购物车
* 题目描述
* 小明去了一家大型商场拿到了一个购物车并开始购物请你设计一个购物车管理器记录商品添加到购物车的信息商品名称和购买数量并在购买结束后打印出商品清单在整个购物过程中小明只有一个购物车实例存在
* 输入描述
* 输入包含若干行每行包含两部分信息分别是商品名称和购买数量商品名称和购买数量之间用空格隔开
* 输出描述
* 输出包含小明购物车中的所有商品及其购买数量每行输出一种商品的信息格式为 "商品名称 购买数量"
* 输入示例
* Apple 3
* Banana 2
* Orange 5
* 输出示例
* Apple 3
* Banana 2
* Orange 5
* 提示信息
* 本道题目请使用单例设计模式
*
* 使用私有静态变量来保存购物车实例
*
* 使用私有构造函数防止外部直接实例化
*/
class ManagerCar{
public HashMap<String, Integer> carHas = null;
public ManagerCar() {
this.carHas = new HashMap<>();
}
public HashMap<String, Integer> getCarHas() {
return carHas;
}
private volatile static ManagerCar managerCar;
public static ManagerCar getManagerCar() {
if (managerCar == null) {
synchronized (ManagerCar.class) {
if (managerCar == null) {
managerCar = new ManagerCar();
}
}
}
return managerCar;
}
}
class ShoppingCart {
private static ShoppingCart instance = null;
private static ArrayList<String> productNames = new ArrayList<>();
private static ArrayList<Integer> productQuantities = new ArrayList<>();
private ShoppingCart() {
}
public static ShoppingCart getInstance() {
if (instance == null) {
synchronized (ManagerCar.class) {
if (instance == null) {
instance = new ShoppingCart();
}
}
}
return instance;
}
public void Add(String name, int quantity) {
productNames.add(name);
productQuantities.add(quantity);
System.out.println(name + " " + quantity);
}
}
class Main1 {
public static void main(String[] args) {
ShoppingCart cart = ShoppingCart.getInstance();
Scanner scanner = new Scanner(System.in);
String inputLine;
while (scanner.hasNextLine()) {
inputLine = scanner.nextLine();
if ("exit".equalsIgnoreCase(inputLine)) {
break;
}
String[] parts = inputLine.split(" ");
if (parts.length == 2) {
String name = parts[0];
int quantity;
try {
quantity = Integer.parseInt(parts[1]);
cart.Add(name, quantity);
} catch (NumberFormatException e) {
System.out.println("输入错误,请重新输入");
}
} else {
System.out.println("输入错误,请重新输入");
}
}
scanner.close();
}
}

View File

@ -0,0 +1,66 @@
package cn.whaifree.designPattern;
public class StrategyPattern {
enum PayType{
ALI(1, "阿里", new AliPayStrategy()),
WX(2,"微信", new WxPayStrategy());
int code;
String name;
PayStrategy payStrategy;
PayType(int code, String name, PayStrategy payStrategy) {
this.code = code;
this.name = name;
this.payStrategy = payStrategy;
}
public static PayStrategy getPayStrategy(Integer code) {
for (PayType value : values()) {
if (value.code == code) {
return value.payStrategy;
}
}
return null;
}
}
interface PayStrategy {
boolean pay();
}
static class AliPayStrategy implements PayStrategy {
@Override
public boolean pay() {
System.out.println("ali");
return false;
}
}
static class WxPayStrategy implements PayStrategy {
@Override
public boolean pay() {
System.out.println("wx");
return false;
}
}
static class PayService {
public void pay(Integer code) {
PayStrategy payStrategy = PayType.getPayStrategy(code);
if (payStrategy != null) {
payStrategy.pay();
}
}
}
public static void main(String[] args) {
new PayService().pay(2);
}
}

View File

@ -0,0 +1,105 @@
package cn.whaifree.leetCode.LinkedList;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiConsumer;
public class LeetCode234 {
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
for (int i = 0; i < 10; i++) {
map.put("1", 1);
}
CountDownLatch countDownLatch = new CountDownLatch(10);
new Thread(new Runnable() {
@Override
public void run() {
map.forEach(
new BiConsumer<String, Integer>() {
@Override
public void accept(String s, Integer integer) {
System.out.println(s + ":" + integer);
}
}
);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
map.put("i" + i, i);
countDownLatch.countDown();
}
}
}).start();
countDownLatch.await();
System.out.println(map.size());
}
@Test
public void test()
{
ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 3, 3,2, 1});
System.out.println(new Solution().isPalindrome(listNode));
}
/**
* 1. 截取一半
* - 逆转对比
* 2. 输出到list中再回问判断
* 3.
*/
class Solution {
public boolean isPalindrome(ListNode head) {
int len = 0;
ListNode index = head;
while (index != null) {
len++;
index = index.next;
}
index = head;
for (int i = 0; i < len / 2; i++) {
index = index.next;
}
ListNode newHead = reverseList(index);
ListNode A = head;
ListNode B = newHead;
while (B != null) {
if (A.val != B.val) {
return false;
}
A = A.next;
B = B.next;
}
return true;
}
private ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}
}

View File

@ -1,7 +1,7 @@
package cn.whaifree.test;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -72,7 +72,7 @@ public class ThreadDemo1 {
static class CyclicBarrierDemo2{
public static void main(String[] args) throws InterruptedException {
// Proxy.newProxyInstance()
// 每9个cyclicBarrier.await()执行后都执行一次wait
CyclicBarrier cyclicBarrier = new CyclicBarrier(9,
() -> System.out.println("上面的9个执行完了,轮到我了 wait complete") // 等待前9次执行结束
@ -116,4 +116,134 @@ public class ThreadDemo1 {
executorService.shutdown();
}
}
}
class p2{
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<Boolean>> ar = new ArrayList<>();
for (int i = 0; i < 10; i++) {
int finalI = i;
Future<Boolean> submit = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
if (finalI == 3) {
throw new MyException();
}
return true;
}
});
ar.add(submit);
}
try {
for (Future<Boolean> submit : ar) {
submit.get();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
if (e.getCause() instanceof MyException){
System.out.println("我捕获到了");
}
}
System.out.println("1");
try {
for (int count = 1; count <= 5; count++) {
if (count == 3) {
//故意制造一下异常
int num = 1 / 0;
} else {
System.out.println("count:" + count + " 业务正常执行");
}
}
} catch (Exception e) {
System.out.println("try catch 在for 外面的情形, 出现了异常for循环显然被中断");
}
for (int count = 1; count <= 5; count++) {
try {
if (count == 3) {
//故意制造一下异常
int num = 1 / 0;
} else {
System.out.println("count:" + count + " 业务正常执行");
}
} catch (Exception e) {
System.out.println("try catch 在for 里面的情形, 出现了异常for循环显然继续执行");
}
}
}
}
class MyException extends RuntimeException {
private static ThreadPoolExecutor resetInstanceExecutor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
public static void main(String[] args) {
List<String> instance = Arrays.asList("i-12345678", "i-23456789", "i-34567890", "i-45678901", "i-56789012",
"i-67890123", "i-78901234", "i-89012345", "i-90123456", "i-101234567");
CountDownLatch countDownLatch = new CountDownLatch(10);
Long startTimestamp = System.currentTimeMillis();
Map<String, Future<Boolean>> resetInstanceMap = new HashMap<>();
boolean exceedRateLimit = false; // 没有被限频
for (String instanceId : instance) {
Future<Boolean> result = resetInstanceExecutor.submit(new ResetInstanceCallable(instanceId,countDownLatch));
resetInstanceMap.put(instanceId, result);
}
for (Map.Entry<String, Future<Boolean>> futureEntry : resetInstanceMap.entrySet()) {
try {
futureEntry.getValue().get();
}
catch (MyException ex){
System.out.println("my exception"); // Callable内抛出的自定义异常无法被捕获Callable内抛出的自定义异常无法被捕获
// get()方法会抛出一个ExecutionException而这个异常的cause才是原始的异常
exceedRateLimit = true;
} catch (InterruptedException e) {
System.out.println("interrupted");
} catch (ExecutionException e) {
System.out.println("exception");
}
}
System.out.println(exceedRateLimit==false? "没有超过限制" : "超过限制");
}
}
class ResetInstanceCallable implements Callable<Boolean> {
private String instanceId;
private CountDownLatch countDownLatch;
public ResetInstanceCallable(String instanceId, CountDownLatch countDownLatch) {
this.instanceId = instanceId;
this.countDownLatch = countDownLatch;
}
@Override
public Boolean call() {
throw new MyException();
}
}