From a2ba0835729439d8b6cc573bd8b54068f9e938bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Mon, 17 Apr 2023 21:56:57 +0800 Subject: [PATCH] =?UTF-8?q?10=E4=B8=AA=E5=BF=85=E9=A1=BB=E6=8E=8C=E6=8F=A1?= =?UTF-8?q?=E7=9A=84lambda=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambdatest/.gitignore | 33 ++++ lambdatest/README.md | 11 ++ lambdatest/pom.xml | 68 +++++++ .../demo/DemoRequestScopeApplication.java | 13 ++ .../com/example/demo/service/TestMain.java | 167 ++++++++++++++++++ .../src/main/resources/application.properties | 1 + lambdatest/src/main/resources/application.yml | 21 +++ 7 files changed, 314 insertions(+) create mode 100644 lambdatest/.gitignore create mode 100644 lambdatest/README.md create mode 100644 lambdatest/pom.xml create mode 100644 lambdatest/src/main/java/com/example/demo/DemoRequestScopeApplication.java create mode 100644 lambdatest/src/main/java/com/example/demo/service/TestMain.java create mode 100644 lambdatest/src/main/resources/application.properties create mode 100644 lambdatest/src/main/resources/application.yml diff --git a/lambdatest/.gitignore b/lambdatest/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/lambdatest/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lambdatest/README.md b/lambdatest/README.md new file mode 100644 index 0000000..c912aec --- /dev/null +++ b/lambdatest/README.md @@ -0,0 +1,11 @@ +# demo-request-scope + +《基于ThreadLocal实现一个上下文管理组件》技术博客对应源码 + +https://juejin.cn/post/7153287656624324638 + +本文基于`ThreadLocal`原理,实现了一个上下文状态管理组件`Scope`,通过开启一个自定义的`Scope`,在`Scope`范围内,可以通过`Scope`各个方法读写数据; + +通过自定义线程池实现上下文状态数据的线程间传递; + +提出了一种基于`Filter`和`Scope`的`Request`粒度的上下文管理方案。 diff --git a/lambdatest/pom.xml b/lambdatest/pom.xml new file mode 100644 index 0000000..83d2dda --- /dev/null +++ b/lambdatest/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.4 + + + com.example + demo-request-scope + 0.0.1-SNAPSHOT + demo-request-scope + Demo project for Spring Boot + + 8 + + + + + + + com.google.guava + guava + 30.1-jre + + + + org.springframework.boot + spring-boot-starter-aop + + + + + com.baomidou + lock4j-redisson-spring-boot-starter + 2.2.4 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lambdatest/src/main/java/com/example/demo/DemoRequestScopeApplication.java b/lambdatest/src/main/java/com/example/demo/DemoRequestScopeApplication.java new file mode 100644 index 0000000..4c0d58d --- /dev/null +++ b/lambdatest/src/main/java/com/example/demo/DemoRequestScopeApplication.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoRequestScopeApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoRequestScopeApplication.class, args); + } + +} diff --git a/lambdatest/src/main/java/com/example/demo/service/TestMain.java b/lambdatest/src/main/java/com/example/demo/service/TestMain.java new file mode 100644 index 0000000..6d4f9e9 --- /dev/null +++ b/lambdatest/src/main/java/com/example/demo/service/TestMain.java @@ -0,0 +1,167 @@ +package com.example.demo.service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +//简化你的代码,提高生产力:这10个Lambda表达式必须掌握 +public class TestMain { + + public static void main(String[] args) { +// 1. 使用Lambda表达式进行集合遍历 +// List list = Arrays.asList("apple", "banana", "orange"); +// for (String fruit : list) { +// System.out.println(fruit); +// } +// +// List list2 = Arrays.asList("apple", "banana", "orange"); +// list2.forEach(fruit -> System.out.println(fruit)); + + + +// 2. 使用Lambda表达式进行排序 +// List list2 = Arrays.asList("apple", "banana", "orange"); +// Collections.sort(list2, new Comparator() { +// public int compare(String s1, String s2) { +// return s1.compareTo(s2); +// } +// }); +// +// List list = Arrays.asList("apple", "banana", "orange"); +// Collections.sort(list, (s1, s2) -> s1.compareTo(s2)); + + + + + + +// 3. 使用Lambda表达式进行过滤 +// List list = Arrays.asList("apple", "banana", "orange"); +// List filteredList = new ArrayList(); +// for (String fruit : list) { +// if (fruit.startsWith("a")) { +// filteredList.add(fruit); +// } +// } +// +// List list2 = Arrays.asList("apple", "banana", "orange"); +// List filteredLists = list2. +// stream(). +// filter(fruit -> fruit.startsWith("a")). +// collect(Collectors.toList()); + + +// 4. 使用Lambda表达式进行映射 +// List list = Arrays.asList("apple", "banana", "orange"); +// List lengths = new ArrayList(); +// for (String fruit : list) { +// lengths.add(fruit.length()); +// } +// +// List lists = Arrays.asList("apple", "banana", "orange"); +// List lengthss = lists.stream().map(fruit -> fruit.length()) +// .collect(Collectors.toList()); + + + + +// 5. 使用Lambda表达式进行归约 +// List list = Arrays.asList(1, 2, 3, 4, 5); +// int sum = 0; +// for (int i : list) { +// sum += i; +// } +// +// List list2 = Arrays.asList(1, 2, 3, 4, 5); +// int sum2 = list2.stream().reduce(0, (a, b) -> a + b); + + + + + + + +// 6. 使用Lambda表达式进行分组 +// List list = Arrays.asList("apple", "banana", "orange"); +// Map> grouped = new HashMap>(); +// for (String fruit : list) { +// int length = fruit.length(); +// if (!grouped.containsKey(length)) { +// grouped.put(length, new ArrayList()); +// } +// grouped.get(length).add(fruit); +// } +// +// +// List lists = Arrays.asList("apple", "banana", "orange"); +// Map> groupeds = lists.stream().collect(Collectors.groupingBy(fruit -> fruit.length())); + + + + +// 7. 使用Lambda表达式进行函数式接口的实现 +// MyInterface myObject = new MyInterface() { +// public void doSomething(String input) { +// System.out.println(input); +// } +// }; +// myObject.doSomething("Hello World"); +// +// MyInterface myObject = input -> System.out.println(input); +// myObject.doSomething("Hello World"); + + + + + + + + //8. 使用Lambda表达式进行线程的创建 +// Thread thread = new Thread(new Runnable() { +// public void run() { +// System.out.println("Thread is running."); +// } +// }); +// thread.start(); +// +// Thread threads = new Thread(() -> System.out.println("Thread is running.")); +// threads.start(); + + + + + +// 9. 使用Lambda表达式进行Optional的操作 +// String str = "Hello World"; +// if (str != null) { +// System.out.println(str.toUpperCase()); +// } +// +// Optional strs = Optional.ofNullable("Hello World"); +// strs.map(String::toUpperCase).ifPresent(System.out::println); + + + + + + +// 10. 使用Lambda表达式进行Stream的流水线操作 + + List list = Arrays.asList("apple", "banana", "orange"); + List filteredList = new ArrayList(); + for (String fruit : list) { + if (fruit.startsWith("a")) { + filteredList.add(fruit.toUpperCase()); + } + } + Collections.sort(filteredList); + + List lists = Arrays.asList("apple", "banana", "orange"); + List filteredLists = lists.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList()); + + + } +} + diff --git a/lambdatest/src/main/resources/application.properties b/lambdatest/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/lambdatest/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/lambdatest/src/main/resources/application.yml b/lambdatest/src/main/resources/application.yml new file mode 100644 index 0000000..31435c0 --- /dev/null +++ b/lambdatest/src/main/resources/application.yml @@ -0,0 +1,21 @@ +spring: + redis: + database: 0 + # Redis服务器地址 写你的ip + host: 192.168.56.10 + # Redis服务器连接端口 + port: 6379 + # Redis服务器连接密码(默认为空) + password: 123456 + # 连接池最大连接数(使用负值表示没有限制 类似于mysql的连接池 + jedis: + pool: + max-active: 200 + # 连接池最大阻塞等待时间(使用负值表示没有限制) 表示连接池的链接拿完了 现在去申请需要等待的时间 + max-wait: -1 + # 连接池中的最大空闲连接 + max-idle: 10 + # 连接池中的最小空闲连接 + min-idle: 0 + # 连接超时时间(毫秒) 去链接redis服务端 + timeout: 6000 \ No newline at end of file