四大函数式接口
Consumer
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
测试:
/**
* Supplier供给型接口,没有参数只有返回值
* @Author Weton Li
* @Date 2021/2/11 14:24
*/
public class Demo4 {
public static void main(String[] args) {
/*
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return null;
}
};*/
Supplier<Integer> supplier = ()->{
System.out.println("执行get()");
return 1024;
};
System.out.println(supplier.get());
}
}
Function
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t); // 传入参数T,返回参数R
测试:
/**
* Function函数型接口,有一个输入参数,有一个输出
* 可用Lambda表达式简化
* @Author Weton Li
* @Date 2021/2/11 12:59
*/
public class Demo1 {
public static void main(String[] args) {
// 工具类,输出输入的值
/*
Function<String, String> function = new Function<String, String>() {
@Override
public String apply(String str) {
return str;
}
};*/
// Lambda使用
Function<String, String> function = (str)->{
return str;
};
System.out.println(function.apply("hello"));
}
}
Predicate
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
测试:
/**
* Predicate断定型接口:有一个输入参数,返回只能是布尔值
* 判断字符串是否为空
* @Author Weton Li
* @Date 2021/2/11 14:18
*/
public class Demo2 {
public static void main(String[] args) {
/*
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String str) {
return str.isEmpty();
}
};*/
Predicate<String> predicate = (str)->{
return str.isEmpty();
};
System.out.println(predicate.test(" "));
}
}
Supplier
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
测试:
/**
* Supplier供给型接口,没有参数只有返回值
* @Author Weton Li
* @Date 2021/2/11 14:24
*/
public class Demo4 {
public static void main(String[] args) {
/*
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return null;
}
};*/
Supplier<Integer> supplier = ()->{
System.out.println("执行get()");
return 1024;
};
System.out.println(supplier.get());
}
}
ForkJoin
并行执行任务,提高效率,必须大数据量情况下使用。
思想:分治
特点:工作窃取
维护的都是双端队列
/**
* @Author Weton Li
* @Date 2021/2/11 17:26
*/
public class Test {
public static void main(String[] args) {
// try {
// test2();
// } catch (ExecutionException e) {
// e.printStackTrace();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
test3();
}
// 普通程序员
public static void test1(){
Long sum = 0l;
long start = System.currentTimeMillis();
for (Long i = 1l; i < 10_0000_0000; i++) {
sum+=i;
}
long end = System.currentTimeMillis();
System.out.println("sum="+"时间"+(end-start));
}
public static void test2() throws ExecutionException, InterruptedException {
long start = System.currentTimeMillis();
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTest forkJoinTest = new ForkJoinTest(0l, 10_0000_0000L);
ForkJoinTask<Long> submit = forkJoinPool.submit(forkJoinTest);
Long sum = submit.get();
long end = System.currentTimeMillis();
System.out.println("sum="+sum+":时间"+(end-start));
}
public static void test3(){
long start = System.currentTimeMillis();
// Stream并行流
// IntStream
// DoubleStream
long sum = LongStream.rangeClosed(0l, 10_0000_0000l).parallel().reduce(0, Long::sum);// ()
long end = System.currentTimeMillis();
System.out.println("sum="+sum+"时间"+(end-start));
}
}
第二种方法
/**
* 求和计算
*
* @Author Weton Li
* @Date 2021/2/11 16:55
*/
public class ForkJoinTest extends RecursiveTask<Long> {
private Long start;
private Long end;
private Long temp = 10000L;
public ForkJoinTest(Long start, Long end) {
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if ((end - start) < temp) {
Long sum = 0L;
for (Long i = start; i <= end; i++) {
sum += i;
}
return sum;
} else {
long middle = (start + end) / 2;
ForkJoinTest task1 = new ForkJoinTest(start, middle);
task1.fork(); // 拆分任务,压入线程队列
ForkJoinTest task2 = new ForkJoinTest(middle + 1, end);
task2.fork();
return task1.join()+task2.join();
}
}
}
异步回调
/**
* 异步调用:ajax
* 异步执行
* 成功回调
* 失败回调
*
* @Author Weton Li
* @Date 2021/2/11 17:55
*/
public class Demo1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 发起一个请求
// CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
// try {
// TimeUnit.SECONDS.sleep(2);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName()+"-runAsync->Void");
// });
// System.out.println("666");
// completableFuture.get();
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "supplyAsync->Integer");
int i = 10 / 0;
return 1024;
});
System.out.println(completableFuture.whenComplete((t, u) -> {
System.out.println("t=>" + t); // 正常的返回结果
System.out.println("u=>" + u);
}).exceptionally((e) -> {
System.out.println(e.getMessage()); // java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
return 404; // 获取到错误的返回结果
}).get());
}
}
JMM
/**
* @Author Weton Li
* @Date 2021/2/11 22:11
*/
public class JMMdemo {
private /*volatile*/ static int num = 0;
public static void main(String[] args) {
new Thread(()->{
// 线程1对主内存的变化不知晓,需给num加volatile,显示可见性
while (num == 0) {
}
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
num =1;
System.out.println(num);
}
}