Skip to content

Instantly share code, notes, and snippets.

@sshaaf
Last active June 20, 2023 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshaaf/bf5c93cfecc768b3a9c1169e63000781 to your computer and use it in GitHub Desktop.
Save sshaaf/bf5c93cfecc768b3a9c1169e63000781 to your computer and use it in GitHub Desktop.
import java.math.BigInteger;
import java.util.concurrent.StructuredTaskScope;
public class ScopedValue21 {
private static final ScopedValue<BigInteger> multiplier = ScopedValue.newInstance();
public static void main(String[] args) throws Exception {
ScopedValue<String> NAME = ScopedValue.newInstance();
ScopedValue.runWhere(NAME, "duke", () -> System.out.println(NAME.get()));
BigInteger number = BigInteger.valueOf(10);
var multipiled = ScopedValue.callWhere(multiplier, number, () -> ScopedValue21.squared());
System.out.println(multipiled);
ScopedValue.runWhere(multiplier, number, () -> {
try (var scope = new StructuredTaskScope<BigInteger>()) {
scope.fork(() -> squared());
scope.fork(() -> squared());
scope.fork(() -> squared());
}
});
}
public static BigInteger squared(){
BigInteger a = multiplier.get();
return a.multiply(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment