Java 具有匿名线程的Reactor调度器

Java 具有匿名线程的Reactor调度器,java,multithreading,project-reactor,reactor,Java,Multithreading,Project Reactor,Reactor,我正在测试反应堆是如何工作的,创建了类似于反应堆文档中的代码 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import reactor.core.publisher.Mono; impor

我正在测试反应堆是如何工作的,创建了类似于反应堆文档中的代码

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ReactorApplicationTests {

  @Test
  public void publishOnThreadTest() {
    Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);

    final Mono<String> mono = Mono.just("Publish on test: \n")
            .map(msg -> msg + "before: " + Thread.currentThread() )
            .publishOn(s)
            .map(msg -> msg + "\nafter: " + Thread.currentThread());

    new Thread(() -> mono.subscribe(System.out::println)).start();
  }
}
import org.junit.Test;
导入org.junit.runner.RunWith;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.test.context.junit4.SpringRunner;
导入reactor.core.publisher.Mono;
导入reactor.core.scheduler.scheduler;
导入reactor.core.scheduler.Schedulers;
@春靴测试
@RunWith(SpringRunner.class)
公共类反应器应用程序测试{
@试验
公共无效PublishOnReadTest(){
调度器s=Schedulers.newParallel(“并行调度器”,4);
final Mono=Mono.just(“测试时发布:\n”)
.map(msg->msg+“在“+Thread.currentThread()之前)
.publishOn(s)
.map(msg->msg+“\n在“+Thread.currentThread()之后);
新线程(()->mono.subscribe(System.out::println)).start();
}
}

我不能让它运行,我做错了什么?使用just subscribe,它可以工作,但我想看看使用的线程,并对其进行一些操作

测试程序不打印任何内容的原因是它过早退出。它应该等到调用subscriber的方法:

@Test
public void publishOnThreadTest() throws InterruptedException {
    Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
    CountDownLatch latch = new CountDownLatch(1);

    final Mono<String> mono = Mono.just("Publish on test: \n")
            .map(msg -> msg + "before: " + Thread.currentThread() )
            .publishOn(s)
            .map(msg -> msg + "\nafter: " + Thread.currentThread());

    new Thread(() -> mono.subscribe((String str) ->{
        System.out.println(str);
        latch.countDown();
    })).start();

    latch.await();
}
@测试
public void publishOnThreadTest()引发InterruptedException{
调度器s=Schedulers.newParallel(“并行调度器”,4);
CountDownLatch闩锁=新的CountDownLatch(1);
final Mono=Mono.just(“测试时发布:\n”)
.map(msg->msg+“在“+Thread.currentThread()之前)
.publishOn(s)
.map(msg->msg+“\n在“+Thread.currentThread()之后);
新线程(()->mono.subscribe((字符串str)->{
系统输出打印项次(str);
倒计时();
})).start();
satch.wait();
}

测试程序不打印任何内容的原因是它过早退出。它应该等到调用subscriber的方法:

@Test
public void publishOnThreadTest() throws InterruptedException {
    Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
    CountDownLatch latch = new CountDownLatch(1);

    final Mono<String> mono = Mono.just("Publish on test: \n")
            .map(msg -> msg + "before: " + Thread.currentThread() )
            .publishOn(s)
            .map(msg -> msg + "\nafter: " + Thread.currentThread());

    new Thread(() -> mono.subscribe((String str) ->{
        System.out.println(str);
        latch.countDown();
    })).start();

    latch.await();
}
@测试
public void publishOnThreadTest()引发InterruptedException{
调度器s=Schedulers.newParallel(“并行调度器”,4);
CountDownLatch闩锁=新的CountDownLatch(1);
final Mono=Mono.just(“测试时发布:\n”)
.map(msg->msg+“在“+Thread.currentThread()之前)
.publishOn(s)
.map(msg->msg+“\n在“+Thread.currentThread()之后);
新线程(()->mono.subscribe((字符串str)->{
系统输出打印项次(str);
倒计时();
})).start();
satch.wait();
}

1。“我不能让它运行”-发生了什么?2.添加导入语句,以便我们可以复制该行为。我无法运行该语句,这意味着没有打印任何结果。它在新线程之外使用上述订阅。“我不能让它运行”-发生了什么?2.添加导入语句,以便我们可以复制该行为。我无法运行该语句,这意味着没有打印任何结果。它在新线程之外使用上述订阅。