首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,095 阅读
2
类的加载
746 阅读
3
Spring Cloud OAuth2.0
728 阅读
4
SpringBoot自动装配原理
694 阅读
5
集合不安全问题
591 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
391
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
391
篇与
的结果
2021-02-12
JUC常用的辅助类
常用的辅助类1、CountDownLatch允许一个或多个线程等待其他线程完成操作package com.sw.assist; import java.util.concurrent.CountDownLatch; /** * @Author suaxi * @Date 2021/2/12 21:41 * <p> * assist 辅助类 */ //减法计数器 public class CountDownLatchTest { public static void main(String[] args) throws InterruptedException { //总数为6 CountDownLatch countDownLatch = new CountDownLatch(6); for (int i = 1; i <= 6; i++) { new Thread(() -> { System.out.println(Thread.currentThread().getName() + "--->走出教室"); countDownLatch.countDown(); //数量减1 }, String.valueOf(i)).start(); } countDownLatch.await(); //等待计数器归零,再向下执行 System.out.println("学生放学离开教室,班长锁门"); } } CountDownLatch原理:countDownLatch.countDown(); //数量减1countDownLatch.await(); //等待计数器归零,再向下执行执行方法,线程调用countDown();,使计数器数量减1,当计数器变为0时,countDownLatch.await();就会被唤醒,继续执行方法2、CyclicBarrier实现一组线程互相等待,当所有的线程都到达某个屏障点后再进行后续的操作package com.sw.assist; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * @Author suaxi * @Date 2021/2/12 21:56 */ //加法计数器 public class CyclicBarrierTest { public static void main(String[] args) { //10个学生都到教室后,老师才开始上课 CyclicBarrier cyclicBarrier = new CyclicBarrier(10, () -> { System.out.println("孙老师开始上课"); }); for (int i = 1; i <= 10; i++) { //注:lambda表达式不能直接操作for循环中的i,它只是简化了new对象的过程 final int temp = i; new Thread(() -> { System.out.println(Thread.currentThread().getName() + "学生" + temp + "进入教室"); try { cyclicBarrier.await(); //等待 } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }).start(); } } } 3、Semaphore可以控制同时访问线程的个数package com.sw.assist; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; /** * @Author suaxi * @Date 2021/2/12 22:10 */ //停车位 public class SemaphoreTest { public static void main(String[] args) { //线程数量:提供3个停车位 Semaphore semaphore = new Semaphore(3); for (int i = 1; i <= 6; i++) { new Thread(() -> { try { semaphore.acquire(); //得到 System.out.println(Thread.currentThread().getName() + "---驶入停车位"); TimeUnit.SECONDS.sleep(3); System.out.println(Thread.currentThread().getName() + "---离开停车位"); } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); //释放 } }, String.valueOf(i)).start(); } } } semaphore.acquire(); //得到,如果当前提供的线程已经满了,则等待,直到线程被释放semaphore.release(); //释放,释放当前的信号量,然后唤醒等待的线程作用:多个共享资源互斥的使用;并发限流(控制最大线程数)
2021年02月12日
51 阅读
0 评论
0 点赞
2021-02-12
Callable()
Callable()可以有返回值,可以抛出异常Callable通过Runnable接口的实现类FetuerTask调用线程package com.sw.callable; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; /** * @Author suaxi * @Date 2021/2/12 21:24 */ public class callableTest { public static void main(String[] args) throws ExecutionException, InterruptedException { MyThread thread = new MyThread(); FutureTask futureTask = new FutureTask(thread); //适配类 new Thread(futureTask,"A").start(); new Thread(futureTask,"B").start(); /* call()方法测试 泛型的参数 = 方法的返回值 相同的两个线程运行后只返回一个结果(有缓存) */ Object o = futureTask.get(); //获取callable返回值,此处的get可能会产生阻塞,接收返回值时可能需要等待 System.out.println(o); } } class MyThread implements Callable<String>{ @Override public String call(){ System.out.println("call()方法测试"); return "泛型的参数 = 方法的返回值"; } }
2021年02月12日
90 阅读
0 评论
0 点赞
2021-02-05
Stream流的使用笔记
Stream流一、遍历/匹配 foreach/find/matchpackage com.sw.demo.stream; import java.util.Arrays; import java.util.List; import java.util.Optional; /** * 1.遍历/匹配(foreach/find/match) */ public class foreach01 { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 77, 5, 4, 33, 677, 86, 43); //遍历输出符合条件的元素 list.stream().filter(x -> x > 6).forEach(System.out::println); //匹配第一个 Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst(); //匹配任意一个元素(适用于并行流) Optional<Integer> findAny = list.stream().filter(x -> x > 6).findAny(); //是否包含符合指定条件的元素 boolean flag = list.stream().anyMatch(x -> x > 6); System.out.println("匹配第一个元素" + findFirst.get()); System.out.println("匹配任意一个元素" + findAny.get()); System.out.println("是否存在大于6的值" + flag); } } 二、筛选 filter按照一定的规则校验流中的元素,将符合条件的元素提取到新的流中package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 2.筛选员工中工资高于8000的人,并形成新的集合 */ public class filter02 { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); List<String> result = personList.stream().filter(x -> x.getSalary()>8000).map(Person::getName).collect(Collectors.toList()); System.out.println("工资大于8000的员工:"+result); } } 三、聚合 max/min/count与MySQL中聚合函数的使用类似package com.sw.demo.stream; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Optional; /** * 3.聚合(max/min/count) */ public class polymerization03 { public static void main(String[] args) { //例1:获取String集合中最长的元素 List<String> list = Arrays.asList("abc", "dcdf", "nihao", "xiexieni", "hello"); Optional<String> max = list.stream().max(Comparator.comparing(String::length)); //System.out.println("最长的元素为:" + max); //例2:获取最大值,并按自定义格式排序 List<Integer> list2 = Arrays.asList(1, 55, 22, 8, 90, 777, 45); //自然排序 Optional<Integer> max01 = list2.stream().max(Integer::compareTo); //自定义排序 Optional<Integer> max02 = list2.stream().max(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }); //System.out.println("自然排序" + max01); //System.out.println("自定义排序" + max02); //例3:获取员工工资最高的人(同理获取String集合中最长的元素) //例4:计算集合中有几个大于6的数 List<Integer> list4 = Arrays.asList(5, 6, 8, 1, 2, 55, 22); long count = list4.stream().filter(x -> x > 6).count(); System.out.println(count); } } 四、映射 map/flatMapmap:接收一个函数作为参数该函数会被应用到每个元素上并将其映射成一个新的元素flatMap:接收一个函数作为参数,并将流中的每个值都换成另一个流,然后把所有流连接成一个新的流package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * 4.映射(map/flatMap) * 映射可以将一个流的元素按照一定的规则映射到另一个流中 * map:接受一个函数作为参数,该函数会被应用到每个函数上,并将其映射成一个新的函数 * flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流 */ public class mapping04 { public static void main(String[] args) { //例1:将数组元素转换为大写/每个元素+3 String[] str = {"abc", "bde", "cfg"}; List<String> list01 = Arrays.stream(str).map(String::toUpperCase).collect(Collectors.toList()); List<Integer> list02 = Arrays.asList(1, 2, 3, 6, 7); List<Integer> plus = list02.stream().map(x -> x + 3).collect(Collectors.toList()); //System.out.println("字符转换为大写:" + list01); //System.out.println("每个值+3:" + plus); //例2:每个员工的薪资上涨1000 List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); //不改变原来员工集合的方式 List<Person> newPersonList = personList.stream().map(person -> { Person newPerson = new Person(person.getName(), 0, 0, null, null); newPerson.setSalary(person.getSalary() + 1000); return newPerson; }).collect(Collectors.toList()); //System.out.println("改动前:" + personList.get(0).getName() + "----->" + personList.get(0).getSalary()); //System.out.println("改动后:" + newPersonList.get(0).getName() + "----->" + newPersonList.get(0).getSalary()); //改变了原来员工集合的方式 List<Person> newPersonList1 = personList.stream().map(person -> { person.setSalary(person.getSalary() + 1000); return person; }).collect(Collectors.toList()); //System.out.println("二次改动前:" + personList.get(0).getName() + "----->" + personList.get(0).getSalary()); //System.out.println("二次改动后:" + newPersonList1.get(0).getName() + "----->" + newPersonList1.get(0).getSalary()); /* 二次改动前:Tom----->9900(直接在personList的基础上操作,没有new对象) 二次改动后:Tom----->9900 */ //例3:将两个字符串合并为1个新的字符串 List<String> list03 = Arrays.asList("a,s,a,s", "a,s,1,2,d"); List<String> newList03 = list03.stream().flatMap(s -> { //将每个元素转换为一个stream String[] split = s.split(","); Stream<String> s01 = Arrays.stream(split); return s01; }).collect(Collectors.toList()); System.out.println("处理集合前:" + list03); System.out.println("处理集合后:" + newList03); /* 处理集合前:[a,s,a,s, a,s,1,2,d] 处理集合后:[a, s, a, s, a, s, 1, 2, d] */ } } 五、归约 reduce把一个流缩减成一个值,实现对集合的求和、乘积、最值等操作package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; /** * 5.规约 * 规约也称缩减,即把一个流缩减为一个值,实现对集合求和、乘积、最值 */ public class reduce05 { public static void main(String[] args) { //例1: List<Integer> list = Arrays.asList(5, 8, 88, 9, 555); //求和方式一: Optional<Integer> sum1 = list.stream().reduce((x, y) -> x + y); //求和方式二: Optional<Integer> sum2 = list.stream().reduce(Integer::sum); //求和方式三: Integer sum3 = list.stream().reduce(0, Integer::sum); //乘积 Optional<Integer> product = list.stream().reduce((x, y) -> x * y); //求最大值方式一 Optional<Integer> max1 = list.stream().reduce((x, y) -> x > y ? x : y); //求最大值方式er Integer max2 = list.stream().reduce(1, Integer::max); //System.out.println("求和:" + sum1.get() + "," + sum2.get() + "," + sum3); //System.out.println("乘积:" + product.get()); //System.out.println("最值:" + max1 + "," + max2); //例2:求所有员工的工资之和,最高工资 List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); //求和1: Optional<Integer> s1 = personList.stream().map(Person::getSalary).reduce(Integer::sum); //方式2: Integer s2 = personList.stream().reduce(0, (s, p) -> s += p.getSalary(), (sa1, sa2) -> sa1 + sa2); //方式3: Integer s3 = personList.stream().reduce(0, (s, p) -> s += p.getSalary(), Integer::sum); //求工资最高的人方式一 Integer smax1 = personList.stream().reduce(0, (m, p) -> m > p.getSalary() ? m : p.getSalary(), Integer::max); //求工资最高的人方式二 Integer smax2 = personList.stream().reduce(0, (m, p) -> m > p.getSalary() ? m : p.getSalary(), (x, y) -> x > y ? x : y); System.out.println("工资之和:"+s1+","+s2+","+s3); System.out.println("工资最值:"+smax1+","+smax2); } } 六、收集 collect1.归集 toList/toSet/toMap因为流不存储数据,在流中的数据处理完之后,要将流中的数据重新归到新的集合里package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.*; import java.util.stream.Collectors; /** * 6.收集(collect) * 将流收集为一个值或一个新的集合 * collect主要依赖java.util.stream.Collectors类内置的静态方法 */ public class collect01 { public static void main(String[] args) { /* 6.1 归集:因为流不存储数据,在流中的数据处理完之后,需要将流中的数据重新归集到新的集合里 toList(),toSet(),toMap() */ //toList/toSet List<Integer> list = Arrays.asList(1, 6, 3, 4, 6, 7, 9, 6, 20); List<Integer> newList = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList()); //去重 Set<Integer> newSet = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet()); List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); Map<?, Person> map = personList.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p)); System.out.println("toList:" + newList); //toList:[6, 4, 6, 6, 20] System.out.println("toSet" + newSet); //toSet[4, 20, 6] System.out.println("toMap:" + map); //key:name - value:Person } } 2.统计 count/averagingCollectors提供了一系列用于数据统计的静态方法计数 count平均值 averagingInt,averagingLong,averagingDouble最值 maxBy,minBy求和 summingInt,summingLong,summingDouble统计以上所有 summarizingInt,summarizingLong,summarizingDoublepackage com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.*; import java.util.stream.Collectors; /** * 6.2 统计(count/averaging) * 计数 count * 平均值 averagingInt averagingLong averagingDouble * 最值 maxBy minBy * 求和 summingInt summingLong summingDouble * 统计以上所有 summarizingInt summarizingLong summarizingDouble */ public class collect02 { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); //求总数 Long count = personList.stream().collect(Collectors.counting()); //平均工资 Double salary = personList.stream().collect(Collectors.averagingDouble(Person::getSalary)); //最高工资 Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compareTo)); //工资之和 Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary)); //统计所有信息 DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary)); System.out.println("员工总数:" + count); System.out.println("平均工资:" + salary); System.out.println("最高工资:" + max); System.out.println("工资之和:" + sum); System.out.println("所有信息:" + collect); } } 3.分组 partitioningBy/groupingBy分区:将stream按条件分为两个map,例:按性别将员工分组分组:将集合分为多个map,例:按地区分组package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collector; import java.util.stream.Collectors; /** * 6.3 分组(partitioningBy/groupingBy) * 分区:将stream按条件分为两个Map * 分组:将集合分为多个Map,如按部门,地区分组 */ public class collect03 { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); //按员工薪资高于8000分组 key:boolean - value:List Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.groupingBy(x -> x.getSalary() > 8000)); //按性别分组 key:String - value:List Map<String, List<Person>> sex = personList.stream().collect(Collectors.groupingBy(Person::getSex)); //先按性别分组,再按地区分组 key:String - value:Map Map<String, Map<String, List<Person>>> area = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea))); System.out.println("薪资高于8000的员工:"+part); System.out.println("按性别分组:"+sex); System.out.println("按地区分组:"+area); } } 4.接合 joining将stream中的元素用特定的连接符连接成一个字符串(如果没有连接符,则直接连接)package com.sw.demo.stream; import java.util.Arrays; import java.util.List; import java.util.stream.Collector; import java.util.stream.Collectors; /** * 6.4 接合(joining) * joining可以将stream中的元素用特定的连接符连接成一个字符串(如果没有连接符,则直接连接) */ public class collect04 { public static void main(String[] args) { List<String> list = Arrays.asList("a", "1", "b", "2", "c", "3", "d", "4"); String joining = list.stream().collect(Collectors.joining("-->")); System.out.println(joining); } } 5.归约 reducingCollectors类提供的reducing方法,相比于stream本身的reduce方法,增加了对自定义归约的支持package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * 6.5 规约(reducing) * Collectors类提供的reducing方法相较于stream本身的reduce方法,增加了对自定义规约的支持 */ public class collect05 { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8200, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); //每位员工增加50块奖金,求发奖金之后所有员工的薪资之和 Integer sum = personList.stream().collect(Collectors.reducing(0, Person::getSalary, (x, y) -> (x + y + 50))); System.out.println("发奖金之后的薪资之和:" + sum); //stream的reduce方法,求员工薪资之和 Optional<Integer> reduceSum = personList.stream().map(Person::getSalary).reduce(Integer::sum); System.out.println("员工薪资之和(reduce方法):"+reduceSum); } } 七、排序 sortedsorted():自然排序,流中元素需实现Comparable接口sorted(Comparator com):自定义排序package com.sw.demo.stream; import com.sw.demo.pojo.Person; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; /** * 7 排序(sort) * sorted():自然排序,流中元素实现Comparable接口 * sorted(Comparator com):Comparator排序器实现自定义排序 */ public class sorted07 { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 18, "male", "昆明")); personList.add(new Person("Jack", 7000, 19, "male", "楚雄")); personList.add(new Person("Lily", 7800, 20, "female", "楚雄")); personList.add(new Person("Anni", 8900, 21, "female", "大理")); personList.add(new Person("Owen", 9500, 22, "male", "昆明")); personList.add(new Person("Alisa", 7900, 22, "female", "保山")); //例:按薪资排序(工资相同的按年龄比较) //升序排列(自然排序) List<String> listASC = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName).collect(Collectors.toList()); //降序排列(升序反转即为降序) List<String> listDESC = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).map(Person::getName).collect(Collectors.toList()); //先按工资,再按年龄升序排列(升序) List<String> listASC01 = personList.stream().sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName).collect(Collectors.toList()); //先按工资再按年龄自定义排序(降序) /* debug流程(降序):两两比较,a>b,a存起来,b继续跟c比较,如果b<c,则继续比较a、c两个数,否则反之 */ List<String> diy = personList.stream().sorted((p1, p2) -> { if (p1.getSalary() == p2.getSalary()) { //System.out.println(p2.getAge() - p1.getAge()); return p2.getAge() - p1.getAge(); } else { //System.out.println(p2.getSalary() - p1.getSalary()); return p2.getSalary() - p1.getSalary(); } }).map(Person::getName).collect(Collectors.toList()); System.out.println("自然排序:"+listASC); System.out.println("工资降序:"+listDESC); System.out.println("先按工资再按年龄自然排序:"+listASC01); System.out.println("自定义排序:"+diy); } } 八、提取/组合concat合并,distinct去重,limit限制,skip跳过package com.sw.demo.stream; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * 8.提取/组合 * 对流进行合并、去重(distinct)、限制(limit)、跳过(skip)等操作 */ public class extract08 { public static void main(String[] args) { String[] arr1 = {"a", "b", "d", "r", "a"}; String[] arr2 = {"g", "r", "d", "b", "c"}; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); //合并两个流,并去重 List<String> concatDistinct = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList()); //limit 限制只能从流中获取前n个数 List<Integer> limit = Stream.iterate(1, x -> x + 1).limit(10).collect(Collectors.toList()); //skip 跳过前n个数据,注:需加上limit限制,否则iterate构造器会一直创建数据 List<Integer> skip = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList()); System.out.println("合并流:" + concatDistinct); System.out.println("限制只能从流中获取前10个数:" + limit); System.out.println("跳过第一个数据:" + skip); } } 注:全文参考https://blog.csdn.net/mu_wind/article/details/109516995
2021年02月05日
226 阅读
0 评论
0 点赞
2021-01-06
集合不安全问题
集合类不安全List不安全package com.sw.unsafe; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; /** * @Author suaxi * @Date 2021/1/4 17:12 * java.util.ConcurrentModificationException 并发修改异常 */ public class ListTest { public static void main(String[] args) { /* 并发情况下ArrayList不安全 解决方案: 1、List<String> list = new Vector<>(); 2、List<String> list = Collections.synchronizedList(new ArrayList<>()); 3、List<String> list = new CopyOnWriteArrayList<>(); CopyOnWrite 写入时复制(是一种优化策略) 多个线程调用的时候可能存在写入覆盖问题,读取没问题 CopyOnWrite在写入的时候复制一份给调用者,调用者写入新的数据完成之 后再把之前复制的数据放回原来的位置,保证线程的安全,以此避免了写入覆盖问题 相较于Vector,它没有采用synchronized锁,而是采用Lock锁,效率更高 */ List<String> list = new CopyOnWriteArrayList<>(); for (int i = 0; i < 100; i++) { new Thread(() ->{ list.add(UUID.randomUUID().toString().substring(0,5)); System.out.println(list); },String.valueOf(i)).start(); } } } Set不安全package com.sw.unsafe; import java.util.*; import java.util.concurrent.CopyOnWriteArraySet; /** * @Author suaxi * @Date 2021/1/4 22:08 * java.util.ConcurrentModificationException 并发修改异常 * 解决方法: * 1、Set<String> set = Collections.synchronizedSet(new HashSet<>()); * 2、Set<String> set = new CopyOnWriteArraySet<>(); */ public class SetTest { public static void main(String[] args) { Set<String> set = new CopyOnWriteArraySet<>(); for (int i = 0; i < 30; i++) { new Thread(() ->{ set.add(UUID.randomUUID().toString().substring(0,5)); System.out.println(set); },String.valueOf(i)).start(); } } } hashSet底层是什么?public HashSet(){ map = new HashMap<>(); } //源码 //set add的本质就是map public boolean add(E e) { return map.put(e, PRESENT)==null; } //PRESENT是静态终类 private static final Object PRESENT = new Object();Map不安全package com.sw.unsafe; import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; /** * @Author suaxi * @Date 2021/1/4 22:26 * java.util.ConcurrentModificationException * 解决方法: * Map<String, String> map = new ConcurrentHashMap<>(); */ public class MapTest { public static void main(String[] args) { //默认等价于? new HashMap<>(16,0.75); Map<String, String> map = new ConcurrentHashMap<>(); for (int i = 0; i < 30; i++) { new Thread(() ->{ map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,5)); System.out.println(map); },String.valueOf(i)).start(); } } }
2021年01月06日
591 阅读
0 评论
0 点赞
2021-01-06
8锁问题
8锁问题什么是锁?锁的对象是谁?package com.sw.lock8; import java.util.concurrent.TimeUnit; /** * @Author suaxi * @Date 2021/1/4 15:44 * 1、执行顺序:发短信 打电话 * 2、增加睡眠时间后,执行顺序:发短信 打电话 */ public class Test1 { public static void main(String[] args) { Phone1 phone1 = new Phone1(); new Thread(() ->{ phone1.sms(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() ->{ phone1.call(); },"B").start(); } } class Phone1{ //synchronized锁的对象是方法的调用者 //两个方法用的是同一个锁,谁先拿到锁,谁先调用 public synchronized void sms(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("发短信"); } public synchronized void call(){ System.out.println("打电话"); } }package com.sw.lock8; import java.util.concurrent.TimeUnit; /** * @Author suaxi * @Date 2021/1/4 15:51 * 3、增加一个hello的普通方法后,执行顺序为 hello 发短信 * 4、两个对象、两个同步方式,执行顺序为 打电话 发短信 */ public class Test2 { public static void main(String[] args) { //两个对象、两个调用者、两把锁 Phone2 phone1 = new Phone2(); Phone2 phone2 = new Phone2(); new Thread(() ->{ phone1.sms(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() ->{ phone2.call(); },"B").start(); } } class Phone2{ //synchronized锁的对象是方法的调用者 public synchronized void sms(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("发短信"); } public synchronized void call(){ System.out.println("打电话"); } //普通方法不受锁的限制 public void hello(){ System.out.println("hello"); } }package com.sw.lock8; import java.util.concurrent.TimeUnit; /** * @Author suaxi * @Date 2021/1/4 15:58 * 5、增加static后,执行顺序为 发短信 打电话 * 6、两个Phone对象,执行顺序依然为为 发短信 打电话 */ public class Test3 { public static void main(String[] args) { //两个对象的Class类模板只有一个,与问题5一样锁的也是Class Phone3 phone = new Phone3(); Phone3 phone1 = new Phone3(); new Thread(() ->{ phone.sms(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() ->{ phone1.call(); },"B").start(); } } class Phone3{ //synchronized锁的对象是方法的调用者 //static静态方法,类一加载就有了,此处锁的是Class对象 public static synchronized void sms(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("发短信"); } public static synchronized void call(){ System.out.println("打电话"); } }package com.sw.lock8; import java.util.concurrent.TimeUnit; /** * @Author suaxi * @Date 2021/1/4 16:05 * 7、1个静态同步方法,1个普通同步方法,一个对象,执行顺序 打电话 发短信 * 8、1个静态同步方法,1个普通同步方法,两个对象,执行顺序 打电话 发短信 */ public class Test4 { public static void main(String[] args) { //两个对象的Class类模板只有一个,与问题5一样锁的也是Class Phone4 phone1 = new Phone4(); Phone4 phone2 = new Phone4(); new Thread(() ->{ phone1.sms(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() ->{ phone2.call(); },"B").start(); } } class Phone4{ //静态同步方法 锁的是Class类模板 public static synchronized void sms(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("发短信"); } //普通同步方法 锁的是调用者,不需要再等待发短信的sleep睡眠时间,两个方法用的不是同一个锁 public synchronized void call(){ System.out.println("打电话"); } }小结一个对象,拿的是同一把锁两个对象拿的是不同的锁加了static静态方法之后,不论有几个对象,锁的只有一个Class模板普通方法不受锁的限制
2021年01月06日
93 阅读
0 评论
0 点赞
2021-01-06
生产者和消费者问题
生产者和消费者问题package com.sw.pc; /** * @Author suaxi * @Date 2021/1/4 14:25 */ public class A { public static void main(String[] args) { Data data = new Data(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"A").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"B").start(); } } class Data{ private int num = 0; //+1 public synchronized void incr() throws InterruptedException { if (num!=0){ //等待 this.wait(); } num++; System.out.println(Thread.currentThread().getName()+"===>"+num); //通知 this.notifyAll(); } //-1 public synchronized void decr() throws InterruptedException { if (num==0){ //等待 this.wait(); } num--; System.out.println(Thread.currentThread().getName()+"===>"+num); //通知 this.notifyAll(); } } 当增加多个线程时(A,B,C,D),会出现虚假唤醒的问题虚假唤醒:线程可以被唤醒,但不会被通知、中断或超时解决方法:将if判断换为whilepackage com.sw.pc; /** * @Author suaxi * @Date 2021/1/4 14:25 */ public class A { public static void main(String[] args) { Data data = new Data(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"A").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"B").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"C").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"D").start(); } } class Data{ private int num = 0; //+1 public synchronized void incr() throws InterruptedException { while (num!=0){ //等待 this.wait(); } num++; System.out.println(Thread.currentThread().getName()+"===>"+num); //通知 this.notifyAll(); } //-1 public synchronized void decr() throws InterruptedException { while (num==0){ //等待 this.wait(); } num--; System.out.println(Thread.currentThread().getName()+"===>"+num); //通知 this.notifyAll(); } } JUC版生产者消费者问题使用了Condition接口下的await()等待和signalAll()通知package com.sw.pc; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Author suaxi * @Date 2021/1/4 14:55 */ public class B { public static void main(String[] args) { Data1 data1 = new Data1(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data1.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"A").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data1.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"B").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data1.incr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"C").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { try { data1.decr(); } catch (InterruptedException e) { e.printStackTrace(); } } },"D").start(); } } class Data1{ private int num = 0; Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); //+1 public void incr() throws InterruptedException { try { lock.lock(); //加锁 while (num!=0){ //等待 condition.await(); } num++; System.out.println(Thread.currentThread().getName()+"===>"+num); //通知 condition.signalAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); //解锁 } } //-1 public void decr() throws InterruptedException { try { lock.lock(); //加锁 while (num==0){ //等待 condition.await(); } num--; System.out.println(Thread.currentThread().getName()+"===>"+num); //通知 condition.signalAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); //解锁 } } }Condition精准通知与线程唤醒可以看到之前的线程执行顺序是无序的。package com.sw.pc; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Author suaxi * @Date 2021/1/4 15:08 * Condition精准通知与线程唤醒 * ABC三个人互相打电话,A->B,B->C,C->A顺序执行 */ public class C { public static void main(String[] args) { Data2 data2 = new Data2(); new Thread(() ->{ for (int i = 0; i < 10; i++) { data2.CallA(); } },"A").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { data2.CallB(); } },"B").start(); new Thread(() ->{ for (int i = 0; i < 10; i++) { data2.CallC(); } },"C").start(); } } class Data2{ private int num = 1; // 1A 2B 3C private Lock lock = new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Condition condition3 = lock.newCondition(); public void CallA(){ try { lock.lock(); //加锁 while (num!=1){ //等待 condition1.await(); } num = 2; System.out.println(Thread.currentThread().getName()+"===>A打完了"); //通知 condition2.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); //解锁 } } public void CallB(){ try { lock.lock(); //加锁 while (num!=2){ //等待 condition2.await(); } num = 3; System.out.println(Thread.currentThread().getName()+"===>B打完了"); //通知 condition3.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); //解锁 } } public void CallC(){ try { lock.lock(); //加锁 while (num!=3){ //等待 condition3.await(); } num = 1; System.out.println(Thread.currentThread().getName()+"===>C打完了"); //通知 condition1.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); //解锁 } } } 通过不同的监视器监视线程来达到精准通知与线程唤醒的目的
2021年01月06日
150 阅读
0 评论
0 点赞
2021-01-06
Lock锁
Lock锁传统synchronized锁package com.sw; /** * @Author suaxi * @Date 2021/1/4 14:00 */ public class SaleTicket01 { public static void main(String[] args) { Ticket1 ticket1 = new Ticket1(); new Thread(() ->{for (int i = 0; i < 30; i++) ticket1.sale();},"A").start(); new Thread(() ->{for (int i = 0; i < 30; i++) ticket1.sale();},"B").start(); new Thread(() ->{for (int i = 0; i < 30; i++) ticket1.sale();},"C").start(); } } //synchronized锁 class Ticket1{ private int num = 30; public synchronized void sale(){ if (num>0){ System.out.println(Thread.currentThread().getName()+"卖了"+(num--)+"张票,剩余:"+num); } } } Lock接口公平锁:先来后到非公平锁:可以插队(默认使用)package com.sw; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Author suaxi * @Date 2021/1/4 11:28 */ public class SaleTicket02 { public static void main(String[] args) { Ticket ticket = new Ticket(); new Thread(() ->{for (int i = 0; i < 30; i++) ticket.sale();},"A").start(); new Thread(() ->{for (int i = 0; i < 30; i++) ticket.sale();},"B").start(); new Thread(() ->{for (int i = 0; i < 30; i++) ticket.sale();},"C").start(); } } //Lock锁 class Ticket{ private int num = 30; //1.新建重入锁 Lock lock = new ReentrantLock(); public void sale(){ lock.lock(); //2.加锁 try{ //业务代码 if (num>0){ System.out.println(Thread.currentThread().getName()+"卖了"+(num--)+"张票,剩余:"+num); } }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); //3.解锁 } } } synchronized与Lock的区别1、synchronized 是内置的Java关键字;Lock是一个Java类2、synchronized 无法判断 获取锁 的状态;Lock可以判断3、synchronized 会自动释放锁;Lock必须手动释放,如果不释放,会产生死锁问题4、synchronized 线程1(获得锁,阻塞),线程2(一直傻傻的等待);Lock锁就不一定会一直等待下去lock.trylock()5、synchronized 可重入锁,不可中断,非公平;Lock 可重入锁,可以判断锁的状态,非公平(可自定义公平性)6、synchronized 适用于锁少量的同步代码问题;Lock 适合大量同步代码问题
2021年01月06日
66 阅读
0 评论
0 点赞
2021-01-03
Redis缓存穿透和雪崩
Redis缓存穿透和雪崩缓存穿透1、概念当用户查询一个数据时,发现redis内存数据库中没有(缓存未命中),于是向持久层数据库(如MySQL)查询,发现也没有要查询数据,造成本次查询请求失败。当用户很多时,出现了同样的情况,就会给持久层数据库造成很大的压力,这个过程就称为缓存穿透。解决方案布隆过滤器:它是一种数据结构,对所有可能查询的参数以hash形式存储,在控制层先进行校验,不符合则丢弃,从而避免对底层数据库的查询压力。缓存空对象:当存储层不命中后,将返回的空对象也存储起来,同时设置一个过期时间,之后有请求访问这个数据时从缓存中获取,以此保护后端数据源存在的问题:1、存储空对象需要更多的存储空间2、即使设置了过期时间,缓存层和存储层的数据存在一定时间的窗口期,对数据一致性要求较高的业务可能会出现问题缓存击穿1、概念:当某个key在过期的瞬间,有大量的并发请求访问(这类数据一般是热点数据),由于缓存过期,请求会去访问数据库来进行查询,并且回写缓存,会导致数据库瞬间压力过大,如:微博热搜宕机解决方案1、设置热点数据永不过期2、加互斥锁使用分布式锁,保证对于每个key只有一个线程去查询后端服务,其他没有获得分布式锁的线程需要等待缓存雪崩1、概念指在某一个时间段,缓存集中过期,查询请求全部落到后端数据库头上。例:双11的抢购,将热点商品集中放入缓存,设置过期时间为1点,到了凌晨1点,这批热点商品的缓存都过期了,这时对于这批商品的查询请求会全部落到数据库头上,对于数据库而言产生了周期性的波峰压力,底层存储的调用量指数上升,整个服务极易出现问题。解决方案1、Redis高可用增加Redis服务器,一台出现问题之后,其他节点正常工作(异地多活)2、限流降级在缓存失效后,通过加锁或队列来控制读取数据库写缓存的线程数量3、数据预热在正式部署之前,把可能的数据先访问一遍,在发生高并发请求之前手动触发缓存key,并设置不同的缓存过期时间,让缓存失效的时间尽量均匀,而不是集中过期
2021年01月03日
273 阅读
0 评论
0 点赞
1
...
29
30
31
...
49