Flux、Mono创建函数

This blog post delves into the world of reactive programming in Java using the Reactor library. It demonstrates various Flux and Mono creations, such as 'create', 'generate', 'just', 'from', 'defer', 'interval', 'empty', 'error', 'never', and 'range'. Key concepts like emission sequences, completion, error handling, and lazy evaluation are exemplified through code snippets.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Flux.create((t) -> {
            t.next("create");
            t.next("create1");
            t.complete();
        }).subscribe(System.out::println);

2.Flux.generate(t -> {
            t.next("generate");
            //注意generate中next只能调用1次
            t.complete();
        }).subscribe(System.out::println);

3.//单个元素
    Flux.just("just").subscribe(System.out::println);
    //多个元素
    Flux.just("just", "just1", "just2").subscribe(System.out::println);

4.//Flux->Flux
        Flux.from(Flux.just("just", "just1", "just2"))
                .subscribe(System.out::println);
        //Mono->Mono
        Flux.from(Mono.just("just")).subscribe(System.out::println);

5.Flux.fromArray(new String[] { "arr", "arr", "arr", "arr" })
                .subscribe(System.out::println); 

6.Set<String> v = new HashSet<>();
    v.add("1");
    v.add("2");
    v.add("3");
Flux.fromIterable(() -> v.iterator()).subscribe(System.out::println);     

7.Flux.defer(() -> Flux.just("just", "just1", "just2"))
        .subscribe(System.out::println);  

8.Flux.interval(Duration.of(500, ChronoUnit.MILLIS))
        .subscribe(System.out::println);
//防止程序过早退出,放一个CountDownLatch拦住
CountDownLatch latch = new CountDownLatch(1);
latch.await();

9.Flux.empty().subscribe(System.out::println);         

10.Flux.error(new RuntimeException()).subscribe(System.out::println);    

11.Flux.never().subscribe(System.out::println);

12.Flux.range(0, 100).subscribe(System.out::println);   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值