eu.lucaventuri.examples.AutoCloseableExample Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fibry Show documentation
Show all versions of fibry Show documentation
The first Java Actor System supporting fibers from Project Loom
package eu.lucaventuri.examples;
import eu.lucaventuri.common.SystemUtils;
import eu.lucaventuri.fibry.Actor;
import eu.lucaventuri.fibry.ActorSystem;
import eu.lucaventuri.fibry.PartialActor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
class C implements AutoCloseable {
AtomicBoolean closed = new AtomicBoolean();
@Override
public void close() throws Exception {
use("close");
closed.set(true);
}
public void use(String reason) {
if (closed.get())
throw new IllegalArgumentException("Resource used after closing: " + reason + "!");
System.out.println("OK: " + reason);
}
}
public class AutoCloseableExample {
public static void main(String[] args) throws Exception {
BiConsumer> actorLogic = (c, thisActor) -> {
c.use("actor first usage");
SystemUtils.sleep(1000);
if (!thisActor.isExiting())
c.use("actor second usage");
};
try (C c = new C(); Actor actor = ActorSystem.anonymous().newActor(actorLogic)) {
c.use("Inside try");
actor.sendMessage(c);
SystemUtils.sleep(500);
}
System.out.println("End of try");
SystemUtils.sleep(1500);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy