Created
March 26, 2025 02:13
-
-
Save hikaMaeng/b2969976f8f33712d52d1ed2d5ad6897 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 dispatcher = Executors.newFixedThreadPool(5); | |
dispatcher.submit(new Callback("A",new Continuation(3, "unlucky"), dispatcher)); | |
dispatcher.submit(new Callback("B",new Continuation(1, "unlucky"), dispatcher)); | |
dispatcher.submit(new Callback("C",new Continuation(5, "unlucky"), dispatcher)); | |
dispatcher.submit(new Callback("D",new Continuation(2, "unlucky"), dispatcher)); | |
} | |
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) {} | |
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