Created
March 11, 2025 14:00
-
-
Save hikaMaeng/935561c813a31bb722fdeff8025f8f01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Objects; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
class Main { | |
public static void main(String[] args){ | |
// var cont = new Continuation(5, "unlucky"); | |
// for (int i = 0; i < 10; i++) { | |
// cont = luck(cont); | |
// System.out.println("my luck :" + cont.ret); | |
// } | |
var pool = Executors.newFixedThreadPool(5); | |
pool.submit(new Callback("A",new Continuation(3, "unlucky"), pool)); | |
pool.submit(new Callback("B",new Continuation(1, "unlucky"), pool)); | |
pool.submit(new Callback("C",new Continuation(5, "unlucky"), pool)); | |
pool.submit(new Callback("D",new Continuation(2, "unlucky"), pool)); | |
} | |
static class Callback implements Runnable{ | |
String name; | |
Continuation cont; | |
ExecutorService pool; | |
Callback(String name, Continuation cont, ExecutorService pool) { | |
this.name = name; | |
this.cont = cont; | |
this.pool = pool; | |
} | |
public void run() { | |
cont = luck(cont); | |
System.out.println(name + ":" + cont.flag + " :" + cont.ret + " at " + Thread.currentThread().getName()); | |
if(!Objects.equals(cont.ret, "lucky")){ | |
pool.submit(this); | |
} | |
} | |
} | |
public record Continuation(int flag, String ret) { | |
static Continuation nothing() { | |
throw new IllegalArgumentException(""); | |
} | |
Continuation next(int flag, String ret) { | |
return new Continuation(flag, ret); | |
} | |
} | |
private static Continuation luck(Continuation cont) { | |
var flag = cont.flag % 7; | |
return switch (flag) { | |
case 0 -> new Continuation(1, cont.flag > 0 ? "lucky" : "unlucky"); | |
default -> new Continuation(flag + 1, "unlucky"); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment