当前位置:   article > 正文

juc--四大函数式接口、stream流式计算、forkJoin分支合并

juc--四大函数式接口、stream流式计算、forkJoin分支合并

juc–四大函数式接口、stream流式计算、forkJoin分支合并

1、四大函数式接口

package com.zy.fourfuncinterface;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * 四大函数式接口
 * Function class
 * Predicate class
 * Consumer class
 * Supplier class
 * @program: JucPro
 * @author: zy
 * @create: 2023-04-24 21:31
 */
public class FourFunctionInterface {

    public static void main(String[] args) {
        // Function:函数型接口,有个输入参数,有个输出
        Function<String, String> function = new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s;
            }
        };
        // Function Lambda表达式简化
        Function<String, String> function2 = (String s) -> s;
        Function<String, String> function3 = Function.identity();

        System.out.println(function.apply("123"));
        System.out.println(function2.apply("456"));
        System.out.println(function3.apply("789"));

        // Predicate:断定型接口,有一个输入参数,返回值只能是布尔值
        Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.equals("123");
            }
        };
        Predicate<String> predicate2 = s -> "456".equals(s);

        System.out.println(predicate.test("123"));
        System.out.println(predicate2.test("456"));

        // Consumer:消费型接口,有一个输入参数,没有返回值
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        Consumer<String> consumer2 = s -> System.out.println(s);

        consumer.accept("123");
        consumer2.accept("456");

        // Supplier:供给型接口,没有参数,只有返回值
        Supplier<String> supplier = new Supplier<String>() {
            @Override
            public String get() {
                return "hello";
            }
        };
        Supplier<String> supplier2 = ()-> "你好";

        System.out.println(supplier.get());
        System.out.println(supplier2.get());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

2、stream流式计算

package com.zy.stream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 * @program: JucPro
 * @author: zy
 * @create: 2023-04-24 22:58
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
package com.zy.stream;

import java.util.Arrays;
import java.util.List;

/**
 * Stream流式计算
 *
 * @program: JucPro
 * @author: zy
 * @create: 2023-04-24 22:55
 */
public class StreamComputing {

    public static void main(String[] args) {
        /*
            1、id必须是偶数
            2、年龄必须大于23岁
            3、用户名转为大写
            4、用户名字母倒着排序
            5、只输出一个用户
         */
        User u1 = new User(1,"a",21);
        User u2 = new User(2,"b",22);
        User u3 = new User(3,"c",23);
        User u4 = new User(4,"d",24);
        User u5 = new User(5,"e",25);
        User u6 = new User(6,"f",26);
        // 集合存储
        List<User> users = Arrays.asList(u1, u2, u3, u4, u5, u6);
        // Stream 流式计算、链式编程、lambda表达式、函数式接口
        users.stream()
                .filter(u->u.getId()%2==0)
                .filter(u->u.getAge()>23)
                .map(u->u.getName().toUpperCase())
                .sorted((name1,name2)->name2.compareTo(name1))
                .limit(1)
                .forEach(System.out::println);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

3、forkJoin

package com.zy.forkjoin;

import java.util.concurrent.RecursiveTask;

/**
 * 分支合并
 * 在JDK1.7,并行执行任务,提高效率,大数据量,把大任务拆分为小任务
 * 特点:工作窃取,这个里面维护的都是双端队列
 * 如何使用:
 *  1.forkJoinPool 通过它来执行
 *  2.计算任务 forkJoinPool.execute(forkJoinTask task)
 *  3.计算类继承forkJoinTask
 * forkJoinTask:
 *  RecursiveAction:递归事件,没有返回值
 *  RecursiveTask:递归任务,有返回值
 * @program: JucPro
 * @author: zy
 * @create: 2023-04-25 21:37
 */
public class StudyForkJoin extends RecursiveTask<Long> {

    private Long start;
    private Long end;

    // 可动态控制该值,继续调优
    private Long critical = 10000L;

    public StudyForkJoin(Long start,Long end){
        this.start = start;
        this.end = end;
    }

    /**
     * 计算方法
     */
    @Override
    protected Long compute() {
        if((end-start)>critical){
            Long sum = 0L;
            for (Long i = start; i <= end; i++) {
                sum+=i;
            }
            return sum;
        }else{
            // 中间值
            long median = (start + end) / 2;
            StudyForkJoin task1 = new StudyForkJoin(start, median);
            // 拆分任务,把任务压入线程队列
            task1.fork();
            StudyForkJoin task2 = new StudyForkJoin(median + 1, end);
            task2.fork();

            return task1.join()+task2.join();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
package com.zy.forkjoin;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * 测试forkJoin
 * @program: JucPro
 * @author: zy
 * @create: 2023-04-25 22:03
 */
public class Test {

    public static void test1(){
        Long sum = 0L;
        long start = System.currentTimeMillis();
        for (Long i = 1L; i <= 20_0000_0000L; i++) {
            sum+=i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum="+sum+"执行时间为:"+(end-start));
    }
    public static void test2(){
        Long sum = 0L;
        StudyForkJoin forkJoin = new StudyForkJoin(0L, 20_0000_0000L);
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        long start = System.currentTimeMillis();

        // 执行,直接返回结果
        sum = forkJoinPool.invoke(forkJoin);
        // 执行,结果阻塞等待
        /*forkJoinPool.execute(forkJoin);
        sum = forkJoin.get();*/
        // 提交,结果阻塞等待
        /*ForkJoinTask<Long> task = forkJoinPool.submit(forkJoin);
        sum = task.get();*/

        long end = System.currentTimeMillis();
        System.out.println("sum="+sum+"执行时间为:"+(end-start));
    }
    public static void test3(){
        long start = System.currentTimeMillis();

        long sum = LongStream.rangeClosed(0L, 20_0000_0000L).parallel().reduce(0, Long::sum);

        long end = System.currentTimeMillis();
        System.out.println("sum="+sum+"执行时间为:"+(end-start));
    }

    public static void main(String[] args) {
        // 普通方法:11042
        //test1();

        // forkJoin:9970
        //test2();

        // stream并行流:292
        test3();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/411651
推荐阅读
相关标签
  

闽ICP备14008679号