puniverse / quasar

Fibers, Channels and Actors for the JVM
http://docs.paralleluniverse.co/quasar/
Other
4.56k stars 575 forks source link

why do I call the constructor multiple times when I use sleep in another thread #341

Closed MiracleJoey closed 3 years ago

MiracleJoey commented 3 years ago

`public class Tester {

static Channel channel;
public static void main(String[]args) throws InterruptedException, SuspendExecution {
    channel = startFiber();
    channel.send( "1");
    channel.send("2");
    channel.send("3");
    Thread.sleep(1000);
    channel.send("4");
    channel.send("5");
    System.out.println("out");
}
public static Channel startFiber() {
    System.out.println("--------------------------------------------------1:" + Thread.currentThread().getName());
    Channel<String> channel = ChannelFactory.newChannel();
    new Fiber("fiber", () -> {
        System.out.println("--------------------------------------------------2:" + Thread.currentThread().getName());
        BASIC_TURN:
        while (true) {
            System.out.println("--------------------------------------------------3:" + Thread.currentThread().getName());
            select(receive(channel));
        }
    }).start();
    return channel;
}

} `

--------------------------------------------------1:main --------------------------------------------------2:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13 out --------------------------------------------------2:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13 --------------------------------------------------3:ForkJoinPool-default-fiber-pool-worker-13

why the second step is executed twice

MiracleJoey commented 3 years ago

The correct use of agents can solve this problem

-javaagent:quasar-core-0.8.0.jar