项目优化建议
程序员文章站
2022-06-05 13:13:50
...
1 bean优化响应式编程
@Data//Lombok自动生成set和get方法,让代码结果简单明了
@Accessors(chain = true)//实现链式调用节省很多时间
public class TestDataVo implements Serializable {
private String name;
private String nick;
private String phone;
public static void main(String[] args) {
TestDataVo testDataVo1 = new TestDataVo().setName("afdasf").setNick("afas");
System.out.println(testDataVo1.name);
System.out.println(testDataVo1.getName());
System.out.println(testDataVo1.nick);
System.out.println(testDataVo1.getNick());
}
}
2 map装载字符串返回
package com.jeeplus.modules.sms.utils;
import java.util.HashMap;
/**
* map工具类
*
*/
public class MapUtils extends HashMap<String, Object> {
/**
* 链式调用装载键值对
* @param key
* @param value
* @return
*/
public MapUtils put(String key, Object value) {
super.put(key, value);
return this;
}
public static void main(String[] args) {
MapUtils utils=new MapUtils();
MapUtils put = utils.put("1", "2").put("15", "2");
//iterating over keys only
for (Object key : put.keySet()) {
System.out.println("Key = " + key);
}
}
}
3 for循环操作数据库(多线程安全处理,缩短操作数据库的时间)
package com.jeeplus.modules.sms.utils.jdk8;
import com.jeeplus.modules.sms.entity.vo.TestDataVo;
import org.apache.commons.lang3.time.DateUtils;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ThreadUtils {
public static void main(String[] args) throws Exception {
// 创建一个固定大小的线程池
ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
Date now = new Date();
Date nextDate = DateUtils.parseDate("2020", "YYYY");
//CompletableFuture组合式异步编程
List<CompletableFuture<Void>> futures = IntStream.range(0, 5).mapToObj(j->CompletableFuture.runAsync(()->{
List<TestDataVo> list = IntStream.range(0, 20)
.mapToObj(i ->new TestDataVo().setName(""+i))
.collect(Collectors.toList());
//数据库操作逻辑
// ...
// ...
}, service)
).collect(Collectors.toList());
//开始等待所有任务执行完成,线程join是可以加入等待有顺序的,如果其他方法用到了,那当前的先执行
CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();
}
}
4 for循环操作集合等,能用并行流parallelStream()尽量用并行流,时间能节省2/3左右,线程不安全可以通过synchronize,加重入锁ReentrantLock ,collect和reduce收集集合数据让操作变得安全
package com.jeeplus.modules.sms.utils.jdk8;
import org.junit.Test;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Stream操作的三个步骤
*
* 创建stream
* 中间操作(过滤、map)
* 终止操作
* 总结就是paralleStream里直接去修改变量是非线程安全的,但是采用collect和reduce操作就是满足线程安全的了。
* 解决办法
* 加同步带吗快sycynize
* 加重入锁ReentrantLock
* 用collect和reduce收集集合数据
*/
public class Stream {
private static List<Integer> list1 = new ArrayList<>();
private static List<Integer> list2 = new ArrayList<>();
private static List<Integer> list3 = new ArrayList<>();
private static Lock lock = new ReentrantLock();
@Test
public void main22() {
List<Integer> numberList = Arrays.asList(1,2,3,4,5,6,7,8,9);
Set<Integer> collect = numberList.parallelStream().map(Integer::intValue).collect(Collectors.toSet());
System.out.println(collect.toArray());
List<Integer> collect1 = numberList.parallelStream().map(Integer::intValue).collect(Collectors.toList());
System.out.println(collect1.toArray());
BinaryOperator<Integer> accumulator=new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
return 2;
}
};
Optional<Integer> reduce = numberList.parallelStream().map(Integer::intValue).reduce(accumulator);
System.out.println(reduce.get());
}
/**
* 线程安全的
*/
@Test
public void main11() {
Collection<Object> collection = new HashSet<Object>();
collection.add("af");
collection.add("afss");
Collection<Object> synchronizedCollection = Collections.synchronizedCollection(collection);
//这种遍历方式因为是异步遍历(线程不安全所以要包装collection),会产生一种情况,就是遍历的顺序是无序的,
// 当然也有相应的好处就是,遍历速度会快,当对生成结果不考虑排序问题而且数据量比较大的时候可以使用.
if (!CollectionUtils.isEmpty(collection)) {
collection.parallelStream().forEach(role -> {
synchronizedCollection.add(role.toString());
synchronizedCollection.forEach(System.out::println);
});
}
}
@Test
public void main0() {
IntStream.range(0, 10000).forEach(list1::add);
IntStream.range(0, 10000).parallel().forEach(list2::add);
IntStream.range(0, 10000).parallel().forEach(i -> {
lock.lock();
try {
list3.add(i);
}finally {
lock.unlock();
}
});
System.out.println("串行执行的大小:" + list1.size());
System.out.println("并行执行的大小:" + list2.size());
System.out.println("加锁并行执行的大小:" + list3.size());
}
public static void main(String[] args) {
// create();
// 创建出一个数组
List<String> strList = Arrays.asList("YangHang", "AnXiaoHei", "LiuPengFei");
//strList.forEach(System.out::println);
strList.stream().forEach(System.out::println);
strList.parallelStream().forEach(System.out::println);
boolean anXiaoHei = strList.stream().anyMatch((a) -> a.equals("AnXiaoHei"));
System.out.println(anXiaoHei);
Optional<String> first = strList.stream().findFirst();
System.out.println(first.get());
boolean anXiaoHei1 = strList.stream().anyMatch((a) -> a.equals("AnXiaoHei"));
System.out.println(anXiaoHei1);
long count = strList.stream().count();
System.out.println(count);
}
/**
* 运行结果:
* For execute time cost 9003 ms
* Stream execute time cost 9054 ms
* ParallelStream execute time cost 2007 ms
* ParallelStream forEachOrdered execute time cost 9013 ms
* parallelStream().forEach是通过多线程并行的方式来执行我们的代码,而parallelStream(). forEachOrdered也是采用多线程,
* 但由于加入了顺序执行约束,故程序是采用多线程同步的方式运行的,最终耗时与for、stream两种单线程执行的耗时接近
*/
@Test
public void main3() throws InterruptedException {
System.out.println("运行结果:");
List<Integer> numberList = Arrays.asList(1,2,3,4,5,6,7,8,9);
//for
Long forBegin = System.currentTimeMillis();
for(Integer number : numberList){
//System.out.println(String.format("For The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
Thread.sleep(1000);
}
System.out.println(String.format("For execute time cost %d ms",System.currentTimeMillis()-forBegin));
System.out.println("\r");
// stream method
Long streamBegin = System.currentTimeMillis();
numberList.stream().forEach(number -> {
//System.out.println(String.format("Stream The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(String.format("Stream execute time cost %d ms",System.currentTimeMillis()-streamBegin));
System.out.println("\r");
// parallelStream method
Long parallelStreamBegin = System.currentTimeMillis();
numberList.parallelStream().forEach(number -> {
//System.out.println(String.format("ParallelStream The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(String.format("ParallelStream execute time cost %d ms",System.currentTimeMillis()-parallelStreamBegin));
System.out.println("\r");
// parallelStream method
Long parallelStreamForEachOrderBegin = System.currentTimeMillis();
numberList.parallelStream().forEachOrdered(number -> {
//System.out.println(String.format("ParallelStream forEachOrdered The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(String.format("ParallelStream forEachOrdered execute time cost %d ms",System.currentTimeMillis()-parallelStreamForEachOrderBegin));
System.out.println("\r");
}
/**
* 运行结果:
* Stream The Current Thread's ID is 1 and output number 1
* Stream The Current Thread's ID is 1 and output number 2
* Stream The Current Thread's ID is 1 and output number 3
* Stream The Current Thread's ID is 1 and output number 4
* Stream The Current Thread's ID is 1 and output number 5
* Stream The Current Thread's ID is 1 and output number 6
* Stream The Current Thread's ID is 1 and output number 7
* Stream The Current Thread's ID is 1 and output number 8
* Stream The Current Thread's ID is 1 and output number 9
*
* ParallelStream The Current Thread's ID is 1 and output number 6
* ParallelStream The Current Thread's ID is 1 and output number 5
* ParallelStream The Current Thread's ID is 12 and output number 3
* ParallelStream The Current Thread's ID is 1 and output number 2
* ParallelStream The Current Thread's ID is 15 and output number 7
* ParallelStream The Current Thread's ID is 13 and output number 8
* ParallelStream The Current Thread's ID is 1 and output number 1
* ParallelStream The Current Thread's ID is 12 and output number 4
* ParallelStream The Current Thread's ID is 15 and output number 9
*
* ParallelStream forEach Ordered The Current Thread's ID is 16 and output number 1
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 2
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 3
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 4
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 5
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 6
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 7
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 8
* ParallelStream forEach Ordered The Current Thread's ID is 14 and output number 9
*/
@Test
public void main2() {
System.out.println("运行结果:");
List<Integer> numberList = Arrays.asList(1,2,3,4,5,6,7,8,9);
// stream method
numberList.stream().forEach(number -> {
System.out.println(String.format("Stream The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
});
System.out.println("\r");
// parallelStream method
numberList.parallelStream().forEach(number -> {
System.out.println(String.format("ParallelStream The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
});
System.out.println("\r");
// parallelStream method
numberList.parallelStream().forEachOrdered(number -> {
System.out.println(String.format("ParallelStream forEach Ordered The Current Thread's ID is %d and output number %d ",Thread.currentThread().getId(),number));
});
System.out.println("\r");
}
/**
*
运行结果:
1 2 3 4 5 6 7 8 9
6 5 3 8 4 2 9 7 1
1 2 3 4 5 6 7 8 9
*/
@Test
public void main() {
List<Integer> numberList = Arrays.asList(1,2,3,4,5,6,7,8,9);
System.out.println("运行结果:");
// stream method
numberList.stream().forEach(number -> {
System.out.print(String.format("%d ",number));
});
System.out.println("\r");
// parallelStream method
numberList.parallelStream().forEach(number -> {
System.out.print(String.format("%d ",number));
});
System.out.println("\r");
// parallelStream method
numberList.parallelStream().forEachOrdered(number -> {
System.out.print(String.format("%d ",number));
});
System.out.println("\r");
}
/**
* stream的创建:
*/
private static void create() {
// 1,校验通过Collection 系列集合提供的stream()或者paralleStream()
List<String> list = new ArrayList<>();
java.util.stream.Stream<String> stream = list.stream();
// 2.通过Arrays的静态方法stream()获取数组流
String[] str = new String[10];
java.util.stream.Stream<String> stream2 = Arrays.stream(str);
}
}