syndicate-java/src/test/java/org/syndicate_lang/actors/example/example1/Main.java

43 lines
1.3 KiB
Java
Raw Normal View History

2020-12-04 18:57:47 +00:00
package org.syndicate_lang.actors.example.example1;
import org.syndicate_lang.actors.Actor;
2021-04-14 19:42:50 +00:00
import org.syndicate_lang.actors.Ref;
2020-12-04 18:57:47 +00:00
public class Main {
public static void main(String[] args) throws InterruptedException {
2020-12-04 22:25:40 +00:00
Actor.convenientLogging();
2021-04-14 19:42:50 +00:00
final var vh = Actor.forEntity(new ValueHolder<>("There"));
2020-12-04 18:57:47 +00:00
vh.getActor().daemonize();
2021-04-14 19:42:50 +00:00
final var m = Actor.forEntity(new Main());
m.async(10, (m_, ac) -> m_.run(ac, vh));
2020-12-04 18:57:47 +00:00
@SuppressWarnings("unchecked")
2020-12-04 22:25:40 +00:00
IValueHolder<String> vv = vh.syncProxy(IValueHolder.class);
2020-12-04 18:57:47 +00:00
System.out.println("Value: " + vv.get());
vv.set("Second");
System.out.println("Value: " + vv.get());
2021-04-14 19:42:50 +00:00
System.out.println("Underlying: " + Ref.from(vv));
2020-12-04 18:57:47 +00:00
Actor.awaitAll();
System.out.println("Overall main returning");
}
private Actor me;
2020-12-04 18:57:47 +00:00
private int greetingCounter = 0;
2021-04-14 19:42:50 +00:00
public void run(Actor me, Ref<ValueHolder<String>> vh) {
this.me = me;
2020-12-04 22:25:40 +00:00
this.greet((String) vh.syncProxy(IValueHolder.class).get());
vh.syncVoid((v, _ac) -> v.set("World"));
me.every(1000, () -> {
if (greetingCounter >= 3) me.stop();
this.greet(vh.sync((v, _ac) -> v.get()).await());
2020-12-04 22:25:40 +00:00
});
2020-12-04 18:57:47 +00:00
}
public void greet(String who) {
me.log().info((greetingCounter++) + ": Hi " + who);
2020-12-04 18:57:47 +00:00
}
}