首页
关于
友链
Search
1
wlop 4K 壁纸 4k8k 动态 壁纸
1,443 阅读
2
Nacos持久化MySQL问题-解决方案
917 阅读
3
Docker搭建Typecho博客
745 阅读
4
滑动时间窗口算法
720 阅读
5
Nginx反向代理微服务配置
689 阅读
生活
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
安全
开发工具
百度网盘
天翼网盘
阿里网盘
登录
Search
标签搜索
java
javase
docker
java8
springboot
thread
spring
分布式
mysql
锁
linux
redis
源码
typecho
centos
git
map
RabbitMQ
lambda
stream
少年
累计撰写
189
篇文章
累计收到
20
条评论
首页
栏目
生活
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
安全
开发工具
百度网盘
天翼网盘
阿里网盘
页面
关于
友链
搜索到
6
篇与
的结果
2022-09-15
JAVA8-Optional API
Optional API一、Optional创建方式前置条件:Insurance对象:public class Insurance { private String name; public String getName() { return name; } }1、empty特点:使用get()方法时会抛出异常:No value present范例:Optional<Insurance> emptyOptional = Optional.<Insurance>empty();使用get()获取结果:抛出异常emptyOptional.get();输出结果:Exception in thread "main" java.util.NoSuchElementException: No value present at java.base/java.util.Optional.get(Optional.java:148)2、of特点:使用get(),不会抛异常范例:Optional<Insurance> ofInsurance = Optional.of(new Insurance()); ofInsurance.get();3、ofNullable特点:上面两者综合,为null是掉empty,不为空调of。3.1、为null时,调get()抛出异常范例: Optional<Insurance> ofNullableOptionalNull = Optional.ofNullable(null); ofNullableOptionalNull.get();输出结果:Exception in thread "main" java.util.NoSuchElementException: No value present3.2、不为null时,调get()不报错范例: Optional<Insurance> ofNullableOptionalNotNull = Optional.ofNullable(new Insurance()); ofNullableOptionalNotNull.get();二、orElseGet、orElse、orElseThrow不管那种方式创建,都适用这几个方法。1、orElseGet说明:不为null就返回值,为null返回一个构造的对象supplier范例:Insurance orElseGetInsurance = ofNullableOptionalNull.orElseGet(Insurance::new); System.out.println(orElseGetInsurance);2、orElse说明:不为null就返回值,否则返回一个引用范例:Insurance orElseInsurance = ofNullableOptionalNull.orElse(new Insurance()); System.out.println(orElseInsurance);3、orElseThrow说明:不为null就返回值,否则返回一个异常对象范例:Insurance orElseThrowInsurance = ofNullableOptionalNull.orElseThrow(RuntimeException::new);输出结果:抛出运行时异常Exception in thread "main" java.lang.RuntimeException at java.base/java.util.Optional.orElseThrow(Optional.java:408)4、orElseThrow说明:不为null就返回值,否则返回一个自定义异常对象范例:ofNullableOptionalNull.orElseThrow(() -> new RuntimeException("yanxizhu Exception"));输出结果:抛出自定义异常Exception in thread "main" java.lang.RuntimeException: yanxizhu Exception三、filter先判断传入predicate是否null,让然后判断predicate传入字段是否存在,不存在:返回this。存在:判断predicate条件是否成立,成立:返回this,不成立:返回空。源码: public Optional<T> filter(Predicate<? super T> predicate) { //1、判断传入参数predicate是否为null Objects.requireNonNull(predicate); //2、判断t.getName是否存在, if (!isPresent()) { //2.1、不存在,返回predicate return this; } else { //2.2、存在:判断predicate条件是否成立,成立返回this,不成立返回empty() return predicate.test(value) ? this : empty(); } }requireNonNull()源码:判断传入T是否为null public static <T> T requireNonNull(T obj) { if (obj == null) throw new NullPointerException(); return obj; }范例:使用前创建Optional对象:1、创建Optinal,get()会抛出异常 Optional<Insurance> emptyOptional = Optional.<Insurance>empty(); 2、of创建,get(),不会抛异常 Optional<Insurance> ofInsurance = Optional.of(new Insurance()); 3、ofNullable:上面两者综合 3.1、为null时,调get()抛出异常:No value present Optional<Insurance> ofNullableOptionalNull = Optional.ofNullable(null); 3.2、不为null时,调get()不报错 Optional<Insurance> ofNullableOptionalNotNull = Optional.ofNullable(new Insurance());使用demo1: Optional<Insurance> insurance = emptyOptional.filter(t -> t.getName() == null); Optional<Insurance> insurance = emptyOptional.filter(t -> t.getName() != null); insurance.get();结果://调用get(),都会报错:No value present: 因为name字段不存在使用demo2:范例1:Optional<Insurance> insurance = ofInsurance.filter(t -> t.getName() == null); insurance.get(); 结果://不报错,因为name字段存在,且满足name==null范例2:Optional<Insurance> insurance = ofInsurance.filter(t -> t.getName() != null); insurance.get(); 结果://报错,因为name虽然存在,但是name!=null,不成立,会返回empty空,使用get()时就会抛出异常:No value present使用demo3: Optional<Insurance> insurance = ofNullableOptionalNull.filter(t -> t.getName() != null); Optional<Insurance> insurance = ofNullableOptionalNull.filter(t -> t.getName() == null); insurance.get();结果://都报错:ofNullableOptional,会走empty创建的Optional,字段不存在,直接get获取值为空,报错:No value present使用demo4:范例1:Optional<Insurance> insurance = ofNullableOptionalNotNull.filter(t -> t.getName() == null); insurance.get(); 结果://不会报错,走of方法创建的Optional,name字段存在,且name==null,所以不报错范例2:Optional<Insurance> insurance = ofNullableOptionalNotNull.filter(t -> t.getName() != null); insurance.get(); 结果://报错,因为虽然name存在,但是name!=null不成立,返回empty,get()就报错了filter,总结:使用时不确定是否为empty,所以直接使用ofNullableOptional创建Optional.四、map使用任何方式创建的Opional,map会将结果再包装成Optionla。范例: //map使用 Optional<String> stringOptional = ofNullableOptionalNotNull.map(t -> t.getName()); //有值则返回,没值则返回给定值 System.out.println(stringOptional.orElse("-1")); //判断值是否存在 System.out.println(stringOptional.isPresent()); //存在值,则打印输出,没有值,不打印。 stringOptional.ifPresent(System.out::println);五、flatMap不会将结果再包装成Optional范例:前置条件 @Data public class Cat { private Optional<Eat> eat; } @Data public class Eat { private String foodName; private Integer weight; }使用: //创建Optional对象 Optional<Cat> cat = Optional.ofNullable(new Cat()); //map将结果包装成Optional Optional<Optional<Eat>> eat = cat.map(c -> c.getEat()); //flatMap不会 Optional<Eat> eat1 = cat.flatMap(c -> c.getEat());map与flatMap区别:map将结果包装成Optional,flatMap不会。六、实列实列1、获取值之前判断值是否为空 public static String getInsuranceName(Insurance insurance) { Optional<String> optional = Optional.ofNullable(insurance).map(Insurance::getName); return optional.orElse("-1"); }实列2、根据用户拿到保险名字package com.example.study.java8.optional; import java.util.Optional; /** * 根据用户拿到保险名字 */ public class NullPointerException { public static void main(String[] args) { //肯定会包空指针异常 // String insuranceNameByPerson = getInsuranceNameByPerson(new Person()); //原始写法,做了空指针判断 String insuranceNameByCheckPerson = getInsuranceNameByCheckPerson(new Person()); // System.out.println(insuranceNameByCheckPerson); //Opional写法 String insuranceNameByOptional = getInsuranceNameByOptional(null); System.out.println(insuranceNameByOptional); } //Opional写法 public static String getInsuranceNameByOptional(Person person) { return Optional.ofNullable(person) .map(Person::getCar) .map(Car::getInsurance) .map(Insurance::getName) .orElse("-1"); } //flatMap不会把结果包装成Optional,map会。 //原始写法判断 public static String getInsuranceNameByCheckPerson(Person person) { if(null != person) { if(null != person.getCar()) { Car car = person.getCar(); if (null != car) { Insurance insurance = car.getInsurance(); if (null != insurance) { return insurance.getName(); } } } } return "UNKNOWN"; } public static String getInsuranceNameByPerson(Person person){ return person.getCar().getInsurance().getName(); } }
2022年09月15日
143 阅读
0 评论
3 点赞
2022-09-11
JAVA8-Stram练手
JAVA8-Stram练手交易员对象:package com.example.study.java8.streams.demo; /** * 交易员 */ public class Trader { private final String name; private final String city; public Trader(String n, String c){ this.name = n; this.city = c; } public String getName(){ return this.name; } public String getCity(){ return this.city; } public String toString(){ return "Trader:"+this.name + " in " + this.city; } } 交易对象package com.example.study.java8.streams.demo; /** * 交易 */ public class Transaction { private final Trader trader; private final int year; private final int value; public Transaction(Trader trader, int year, int value){ this.trader = trader; this.year = year; this.value = value; } public Trader getTrader(){ return this.trader; } public int getYear(){ return this.year; } public int getValue(){ return this.value; } public String toString(){ return "{" + this.trader + ", " + "year: "+this.year+", " + "value:" + this.value +"}"; } } 需求说明:1、交易年未2011年,并按交易金额排序2、获取城市并去重3、获取交易员所在城市为“Cambridge”的交易员,去重,并按交易员名字排序4、获取所有交易员名字,并排序,拼接成字符串5、交易员城市是否有在Milan的6、打印所有交易值,且交易员所在城市是Milan的7、找最大的值8、找最小的值需求实现范例:package com.example.study.java8.streams.demo; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Optional; import static java.util.stream.Collectors.toList; /** * 交易员进行交易 */ public class StreamInAction { public static void main(String[] args) { Trader raoul = new Trader("Raoul", "Cambridge"); Trader mario = new Trader("Mario", "Milan"); Trader alan = new Trader("Alan", "Cambridge"); Trader brian = new Trader("Brian", "Cambridge"); List<Transaction> transactions = Arrays.asList( new Transaction(brian, 2011, 300), new Transaction(raoul, 2012, 1000), new Transaction(raoul, 2011, 400), new Transaction(mario, 2012, 710), new Transaction(mario, 2012, 700), new Transaction(alan, 2012, 950) ); //1、交易年未2011年,并按交易金额排序 transactions.stream() .filter(t -> t.getYear() == 2011) .sorted(Comparator.comparing(Transaction::getValue)) .collect(toList()) .forEach(System.out::println); System.out.println("==================================="); //2、获取城市并去重 transactions.stream() .map(t -> t.getTrader().getCity()) .distinct() .forEach(System.out::println); System.out.println("==================================="); //3、获取交易员所在城市为“Cambridge”的交易员,去重,并按交易员名字排序 transactions.stream() .map(t->t.getTrader()) .filter(g->"Cambridge".equals(g.getCity())) .distinct() .sorted(Comparator.comparing(Trader::getName)) .forEach(System.out::println); System.out.println("==================================="); //4、获取所有交易员名字,并排序,拼接成字符串 String result = transactions.stream() .map(t -> t.getTrader().getName()) .distinct() .sorted() .reduce("", (str1, str2) -> str1 + "\t" +str2); System.out.println(result); System.out.println("==================================="); //5、交易员城市是否有在Milan的 boolean anyMatch = transactions.stream() .anyMatch(t -> "Milan".equals(t.getTrader().getCity())); System.out.println(anyMatch); //或者 System.out.println("==================================="); boolean anyMatchMap = transactions.stream() .map(t -> t.getTrader()) .anyMatch(n -> "Milan".equals(n.getCity())); System.out.println(anyMatchMap); System.out.println("==================================="); //6、打印所有交易值,且交易员所在城市是Milan的。 transactions.stream() .filter(t->"Cambridge".equals(t.getTrader().getCity())) .map(Transaction::getValue) .sorted() .forEach(System.out::println); //7、找最大的值 System.out.println("==================================="); Optional<Integer> maxValue = transactions.stream() .map(Transaction::getValue) .reduce((i, j) -> i > j ? i : j); System.out.println(maxValue.get()); //8、找最小的值 System.out.println("==================================="); Optional<Integer> minValue = transactions.stream().map(t -> t.getValue()).reduce(Integer::min); System.out.println(minValue.get()); } }
2022年09月11日
122 阅读
0 评论
1 点赞
2022-09-08
JAVA8-Stream 数值格式
Stream数据类型1、mapToInt、mapToLong、mapToDouble可以将包装类拆箱成基本数据类型,节约内存空间范例:求和方式一:reduce实现 //方式一:reduce实现 List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); list.stream().reduce(Integer::sum).ifPresent(System.out::println); 方式二:mapToInt //方式二:mapToInt list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); IntStream mapToInt = list.stream().mapToInt(i -> i.intValue()); int sum = mapToInt.sum(); System.out.println(sum);方式三:mapToInt后用reduce实现 //方式三:mapToInt后用reduce实现 list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); IntStream intStream = list.stream().mapToInt(i -> i.intValue()); int reduce = list.stream().mapToInt(i -> i.intValue()).reduce(0, (i, j) -> i + j); System.out.println(reduce);区别:直接使用reduce实现,使用的是包装类Integer,用mapToInt则可以转成int计算,占用内存更小。2、mapToObj可以将数据直接转成对象,代码量少很多。范例:需求:1--100,中满足勾股定律的数字 3\4\5 9,并返回成一个数组。1、原始分步骤写法: int a = 9; //创建1-100的数字 IntStream rangeClosed = IntStream.rangeClosed(1, 100); //找到满足勾股定律的数字 Stream<Integer> boxed = rangeClosed.filter(i -> Math.sqrt(a * a + i * i) % 1 == 0).boxed(); //将数字转变成int数组返回 Stream<int[]> stream = boxed.map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)}); //输出数组数据 stream.forEach(array->System.out.println("a="+array[0]+",b="+array[1]+",c="+array[2]));2、原始写法,代码简化后 int a = 9; IntStream.rangeClosed(1, 100) .filter(i->Math.sqrt(a*a + i*i)%1==0) .boxed() .map(b->new int[]{a,b,(int)Math.sqrt(a*a+b*b)}) .forEach(array->System.out.println("a="+array[0]+",b="+array[1]+",c="+array[2]));3、mapToObj实现 int a = 9; IntStream.rangeClosed(1, 100) .filter(i->Math.sqrt(a*a + i*i)%1==0) .mapToObj(b->new int[]{a,b,(int)Math.sqrt(a*a+b*b)}) .forEach(array->System.out.println("a="+array[0]+",b="+array[1]+",c="+array[2]));输出结果:a=9,b=12,c=15 a=9,b=40,c=41使用mapToObj,代码更简洁,直接将满足条件数据,转成一个数组对象。
2022年09月08日
86 阅读
0 评论
2 点赞
2022-09-07
JAVA8-Stream API
二、Stream API:filter、distinct、skip、limit被操作数据List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1);1、filter 过滤范例://filter 过滤 List<Integer> filterResult = list.stream().filter(i -> i % 2 == 0).collect(toList()); filterResult.stream().forEach(System.out::println);2、distinct 去重范例: //distinct 去重 List<Integer> distinctResult = list.stream().distinct().collect(toList()); distinctResult.stream().forEach(System.out::println);3、skip 截断范例: //skip 截断(跳过前面几个,超过长度,直接返回空) List<Integer> skipResult = list.stream().skip(5).collect(toList()); skipResult.stream().forEach(System.out::println);4、limit 查询几条范例: //limit 查询几条 List<Integer> limitResult = list.stream().limit(3).collect(toList()); limitResult.stream().forEach(System.out::println);二、Stream API:Map1、map 数据处理被操作数据List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH)); String[] arrays = {"Hello","world"};范例1: //1、map List<Integer> mapResult = list.stream().map(i -> i * 2).collect(toList()); mapResult.stream().forEach(System.out::println);范例2: //2、map menu.stream().map(Dish::getName).forEach(System.out::println);范例3://3、flatMap 扁平化 //拆分成String[]: {h,e,l,l,o},{w,o,r,l,d} Stream<String[]> splitStream = Arrays.stream(arrays).map(s -> s.split("")); //flatMap扁平化,将{h,e,l,l,o},{w,o,r,l,d}每个数组转成Stream<String> Stream<String> stringStream = splitStream.flatMap(Arrays::stream); stringStream.distinct().forEach(System.out::println);打印结果:H e l o w r d三、Stream API:Match1、Match匹配范例:1.1、allMatch 全部满足条件 Stream<Integer> list = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); //1、allMatch 全部满足条件 boolean booleanAnyMath = list.allMatch(i -> i > 0); System.out.println(booleanAnyMath);1.2、anyMatch 任意一个满足条件 //2、anyMatch 任意一个满足条件 list = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); boolean anyMatch = list.anyMatch(i -> i > 9); System.out.println(anyMatch);1.3、noneMatch 没有一个满足 //3、noneMatch 没有一个满足 list = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); boolean noneMatch = list.noneMatch(i -> i == 11); System.out.println(noneMatch);四、Stream API:find2、find查找范例:2.1、findFirst 查找第一个 //1、findFirst 查找第一个 Optional<Integer> firstOptional = stream.filter(i -> i % 2 == 0).findFirst(); System.out.println(firstOptional); ```` **2.2、findAny 查找任意一个** stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); Optional<Integer> anyOptional = stream.filter(i -> i % 2 == 0).findAny(); System.out.println(anyOptional.get()); **2.3、没找到时,使用get()会直接抛出异常提示信息,会提示错误:No value present** ```java stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); Optional<Integer> noNumOptional = stream.filter(i -> i == 11).findAny(); System.out.println(noNumOptional.get());抛出异常提示: //抛出异常提示:No value present ```` **2.4、没找到时,Optional输出:Optional.empty,不抛异常** System.out.println(noNumOptional); *输出结果:*Optional.empty **3、orElse 如果没查找返回给定的值** 范例: **需求:查询数组中是否包含指定值,没有返回默认值。** ***3.1、原始写法*** 先写个方法,然后调用/* * 原始写法,查询数组中是否包含指定值,没有返回默认值 * @param values * @param defaultValue * @param predicate * @return */ public static int findDifineValue(Integer[] values, int defaultValue, Predicate<Integer> predicate){ for(int i : values){ if(predicate.test(i)){ return i; } } return defaultValue; } 调用原始写法: int findValueResult = findDifineValue(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, -1, i -> i == 9); System.out.println(findValueResult); ***3.2、orElse写法*** // orElse写法 如果没查找返回给定的值 stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); Optional<Integer> noNumOptional = stream.filter(i -> i == 11).findAny(); System.out.println(noNumOptional.orElse(-1));比原始写法简写很多。 ---------- **4、isPresent 判断是否存在,直接返回boolean值** stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); boolean isPresent = stream.filter(i->i==4).findAny().isPresent(); System.out.println(isPresent); **5、存在就打印出来,没有则不打印** stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); stream.filter(i->i==6).findAny().ifPresent(System.out::println); ** 6、还可以进行二次过滤** stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); //先Stream的filter过滤,然后得到Optional,再用Optional的filter过滤 stream.filter(i->i>8).findAny().filter(i->i<10).ifPresent(System.out::println);## 五、Stream API:reduce:聚合作用,根据reduce传入的Function条件进行聚合 ![reduce](https://cdn.jsdelivr.net/gh/willxwu/CDN@main/images/202209081919342.png) 范例:package com.example.study.java8.streams;import java.util.Arrays;import java.util.List;/**Stream reduce:聚合作用,根据reduce传入的Function条件进行聚合用法:只要reduce里面参数满足funcion就可以 */public class StreamReduce {public static void main(String[] args) { //用法:只要reduce的参数满足Function就可以 //reduce(0, (i, j) -> i + j) List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); Integer reduceResult = list.stream().reduce(0, (i, j) -> i + j); System.out.println(reduceResult); System.out.println("==================================="); //reduce((i,j)->i+j) list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); list.stream().reduce((i,j)->i+j).ifPresent(System.out::println); System.out.println("==================================="); //reduce(Math::max) list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); list.stream().reduce(Math::max).ifPresent(System.out::println); System.out.println("==================================="); //reduce(Integer::sum) list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,6,5,4,3,2,1); list.stream().reduce(Integer::sum).ifPresent(System.out::println); }}
2022年09月07日
202 阅读
0 评论
5 点赞
2022-04-24
线程安全-同步容器
线程安全-同步容器主要包括ArrayList -> Vector,Stack HashMap->HashTable(key、value不能为null)Collections.synchronizedXXX(List、Set、Map)都是使用Synchronized进行修饰的,性能不是特别好。可以使用并发容器代替。注意:同步容器也可能是线程步安全的。同步容器线程步安全,代码示例:package com.yanxizhu.demo.concurrency.synContainer; import com.yanxizhu.demo.concurrency.annotation.UnThreadSafety; import java.util.Vector; /** * @description: 同步容器,也可能出现线程不安全情况 * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 10:32 * @version: 1.0 */ @UnThreadSafety public class DemoVecotNo { private static final Vector<Integer> vector = new Vector<>(); public static void main(String[] args) { //一直循环 while(true) { //向vector容器放入值 for(int i=0; i <10; i++){ vector.add(i); } //线程1,向vector容器移除值 new Thread(()->{ for(int i=0; i <vector.size(); i++){ vector.remove(i); } }).start(); //线程2,向vector中获取值 new Thread(()->{ for(int i=0; i <vector.size(); i++){ vector.get(i); } }).start(); } } }输出结果:Exception in thread "Thread-478" Exception in thread "Thread-1724" Exception in thread "Thread-1918" Exception in thread "Thread-1769" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10 at java.base/java.util.Vector.remove(Vector.java:875) at com.yanxizhu.demo.concurrency.synContainer.DemoVecotNo.lambda$main$0(DemoVecotNo.java:27) at java.base/java.lang.Thread.run(Thread.java:834) java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 62 at java.base/java.util.Vector.remove(Vector.java:875) at com.yanxizhu.demo.concurrency.synContainer.DemoVecotNo.lambda$main$0(DemoVecotNo.java:27) at java.base/java.lang.Thread.run(Thread.java:834) java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2 at java.base/java.util.Vector.get(Vector.java:781) at com.yanxizhu.demo.concurrency.synContainer.DemoVecotNo.lambda$main$1(DemoVecotNo.java:34) at java.base/java.lang.Thread.run(Thread.java:834) java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10 at java.base/java.util.Vector.remove(Vector.java:875) at com.yanxizhu.demo.concurrency.synContainer.DemoVecotNo.lambda$main$0(DemoVecotNo.java:27) at java.base/java.lang.Thread.run(Thread.java:834) Exception in thread "Thread-4507" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 12 at java.base/java.util.Vector.get(Vector.java:781) at com.yanxizhu.demo.concurrency.synContainer.DemoVecotNo.lambda$main$1(DemoVecotNo.java:34) at java.base/java.lang.Thread.run(Thread.java:834)说明:add、remove、get防范都是通过synchronized修饰了的,为什么还是会出现线程不安全,因为当remove和get运行时,如果i相等,remove移除i时,get再获取i就包错了。Vector线程安全代码示例package com.yanxizhu.demo.concurrency.synContainer; import com.yanxizhu.demo.concurrency.annotation.ThreadSafety; import lombok.extern.slf4j.Slf4j; import java.util.Vector; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @description: 线程安全容器:Vector * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 10:18 * @version: 1.0 */ @Slf4j @ThreadSafety public class DeomVecotr { private static Vector<Integer> vector = new Vector<>(); //用户数量 private static final int clientsTotal = 5000; //并发数量 private static final int concurrencyTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); //信号量 final Semaphore semaphore = new Semaphore(concurrencyTotal); //闭锁 final CountDownLatch countDownLatch = new CountDownLatch(clientsTotal); for (int i = 0; i < clientsTotal; i++) { final int count = i; executorService.execute(()->{ try { semaphore.acquire(); update(count); semaphore.release(); } catch (InterruptedException e) { log.error("出现错误:【{}】", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("vector size:{}", vector.size()); } /** * 通过线程安全对象dateTimeFormatter处理 */ private static void update(int count){ vector.add(count); } }输出结果:每次是输出5000Hashtable线程安全,代码示例:package com.yanxizhu.demo.concurrency.synContainer; import com.yanxizhu.demo.concurrency.annotation.ThreadSafety; import com.yanxizhu.demo.concurrency.annotation.UnThreadSafety; import lombok.extern.slf4j.Slf4j; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @description: 线程安全容器:Hashtable * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 9:52 * @version: 1.0 */ @Slf4j @ThreadSafety public class DemoHashTable { private static Map<Integer, Integer> map = new Hashtable<>(); //用户数量 private static final int clientsTotal = 5000; //并发数量 private static final int concurrencyTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); //信号量 final Semaphore semaphore = new Semaphore(concurrencyTotal); //闭锁 final CountDownLatch countDownLatch = new CountDownLatch(clientsTotal); for (int i = 0; i < clientsTotal; i++) { final int count = i; executorService.execute(()->{ try { semaphore.acquire(); update(count); semaphore.release(); } catch (InterruptedException e) { log.error("出现错误:【{}】", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("map size:{}", map.size()); } private static void update(int count){ map.put(count, count); } }输出结果:每次都是5000.线程安全。Collections.synchronizedList线程安全,同步容器package com.yanxizhu.demo.concurrency.synContainer; import com.yanxizhu.demo.concurrency.annotation.ThreadSafety; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @description: 安全容器,Collections下的同步容器,线程安全 * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 10:47 * @version: 1.0 */ @Slf4j @ThreadSafety public class DemoCollections { private static List<Integer> list = Collections.synchronizedList(new ArrayList<>()); //用户数量 private static final int clientsTotal = 5000; //并发数量 private static final int concurrencyTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); //信号量 final Semaphore semaphore = new Semaphore(concurrencyTotal); //闭锁 final CountDownLatch countDownLatch = new CountDownLatch(clientsTotal); for (int i = 0; i < clientsTotal; i++) { final int count = i; executorService.execute(()->{ try { semaphore.acquire(); update(count); semaphore.release(); } catch (InterruptedException e) { log.error("出现错误:【{}】", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("list size:{}", list.size()); } /** * 通过线程安全对象dateTimeFormatter处理 */ private static void update(int count){ list.add(count); } }输出结果:每次输出5000,线程安全。Collections.synchronizedSet线程安全,同步容器,代码示例package com.yanxizhu.demo.concurrency.synContainer; import com.google.common.collect.Sets; import com.yanxizhu.demo.concurrency.annotation.ThreadSafety; import lombok.extern.slf4j.Slf4j; import java.util.Collections; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @description: 安全容器,Collections下的同步容器,synchronizedSet,线程安全 * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 10:47 * @version: 1.0 */ @Slf4j @ThreadSafety public class DemoCollectionsSynSet { private static Set<Integer> set = Collections.synchronizedSet(Sets.newHashSet()); //用户数量 private static final int clientsTotal = 5000; //并发数量 private static final int concurrencyTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); //信号量 final Semaphore semaphore = new Semaphore(concurrencyTotal); //闭锁 final CountDownLatch countDownLatch = new CountDownLatch(clientsTotal); for (int i = 0; i < clientsTotal; i++) { final int count = i; executorService.execute(()->{ try { semaphore.acquire(); update(count); semaphore.release(); } catch (InterruptedException e) { log.error("出现错误:【{}】", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("set size:{}", set.size()); } /** * 通过线程安全对象dateTimeFormatter处理 */ private static void update(int count){ set.add(count); } }输出结果:每次输出5000,线程安全Collections.synchronizedMap线程安全同步容器,代码示例:package com.yanxizhu.demo.concurrency.synContainer; import com.yanxizhu.demo.concurrency.annotation.UnThreadSafety; import lombok.extern.slf4j.Slf4j; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @description: 安全容器,Collections.synchronizedMap,线程安全 * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 9:52 * @version: 1.0 */ @Slf4j @UnThreadSafety public class DemoCollectionsSynHashMap { private static Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap()); //用户数量 private static final int clientsTotal = 5000; //并发数量 private static final int concurrencyTotal = 200; public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); //信号量 final Semaphore semaphore = new Semaphore(concurrencyTotal); //闭锁 final CountDownLatch countDownLatch = new CountDownLatch(clientsTotal); for (int i = 0; i < clientsTotal; i++) { final int count = i; executorService.execute(()->{ try { semaphore.acquire(); update(count); semaphore.release(); } catch (InterruptedException e) { log.error("出现错误:【{}】", e.getMessage()); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("map size:{}", map.size()); } private static void update(int count){ map.put(count, count); } }输出结果:每次输出5000,线程安全。常用操作错误package com.yanxizhu.demo.concurrency.synContainer; import java.util.Iterator; import java.util.Vector; /** * @description: 常见容器使用错误,解决办法 * @author: <a href="mailto:vip@foxmail.com">清风</a> * @date: 2022/4/24 11:04 * @version: 1.0 */ public class DemoErrorUser { //增强for循环中修改,报错:java.util.ConcurrentModificationException public static void test1(Vector<Integer> vector) { for (Integer integer: vector) { //使用增强for循环 if (integer.equals(3)) { // vector.remove(integer); vector.add(4); } } } //迭代器中循环修改,报错:java.util.ConcurrentModificationException public static void test2(Vector<Integer> vector) { Iterator<Integer> iterator = vector.iterator(); while (iterator.hasNext()){ Integer integer = iterator.next(); if( integer.equals(3)) { vector.remove(integer); } } } //普通for循环中修改,正常。 public static void test3(Vector<Integer> vector) { for(int i=0; i< vector.size(); i++) { if(i==3) { vector.remove(i); } } } //总结:增强for、迭代器循环中修改,可以先标记,然后最后进行修改。 public static void main(String[] args) { Vector<Integer> vector = new Vector<>(); vector.add(1); vector.add(2); vector.add(3); test1(vector); // test2(vector); // test3(vector); } }输出结果:ConcurrentModificationExceptionException in thread "main" java.util.ConcurrentModificationException at java.base/java.util.Vector$Itr.checkForComodification(Vector.java:1321) at java.base/java.util.Vector$Itr.next(Vector.java:1277) at com.yanxizhu.demo.concurrency.synContainer.DemoErrorUser.test1(DemoErrorUser.java:16) at com.yanxizhu.demo.concurrency.synContainer.DemoErrorUser.main(DemoErrorUser.java:52)增强for、迭代器中循环修改线程报错,正常for循环,正常,解决办法,通过标记后,再单独进行移除操作。
2022年04月24日
209 阅读
0 评论
2 点赞
2022-02-28
JAVA map遍历
Map集合的2种循环方式。一种是通过map.entrySet()。另一种是map.keySet()。所有的集合都可以通过迭代器Iterator,进行遍历。package learn.javase; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 重要 * 遍历map的两种方式map.entrySet() map.keySet() * @author Jole * */ public class MapDemo { public static void main(String[] args) { Map<Integer ,String > map = new HashMap(); map.put(1, "111"); map.put(2, "2222"); //方式一 entrySet() Set<Map.Entry <Integer, String>> sets = map.entrySet(); //获取值1,通过for for(Map.Entry <Integer, String> entry : sets) { System.out.println("ssss:"+entry); System.out.println("key:"+entry.getKey()+"value:"+entry.getValue()); } //获取值2,通过iterator Iterator <Map.Entry <Integer, String>> itt = sets.iterator(); while(itt.hasNext()) { Map.Entry <Integer, String> entrys = itt.next(); System.out.println(entrys.getKey()+"----"+entrys.getValue()); } System.out.println(map.put(3, "222")); //方式二 keySet() Set<Integer> set = map.keySet(); Iterator it = set.iterator(); while(it.hasNext()) { System.out.println("Iterator="+it.next()); System.out.println("key:"+map.get(it.next())); } for(Integer in : set) { System.out.println(in); } } }
2022年02月28日
293 阅读
0 评论
2 点赞