<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div dir="ltr"></div><div dir="ltr">I would create a top-level facade method that loads the products that takes an array and hides the scope details rather than writing it like that. </div><div dir="ltr"><br><blockquote type="cite">On Sep 11, 2023, at 4:09 PM, David <david.vlijmincx@gmail.com> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr">Hi,<br><br>I have a question about where the business logic / shutdown policy is supposed to be when you extend from a StructuredTaskScope. Looking at online examples it seems like the business logic is supposed to be in the handleComplete method of a class that extends StructuredTaskScope, but is this not too far away from the place it is being used?<div><br></div><div>Currently, I am using the following scope as it keeps the shutdown policy closer to where the scope is used in my code, but I do not know if this is the correct way of creating your own scopes. </div><div><br></div><div><div><pre style="color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">class </span><span style="color:rgb(0,0,0)">TriggerScope</span><<span style="color:rgb(0,126,138)">T</span>> <span style="color:rgb(0,51,179)">extends </span><span style="color:rgb(0,0,0)">StructuredTaskScope</span><<span style="color:rgb(0,126,138)">T</span>> {<br><br> <span style="color:rgb(0,51,179)">final private </span><span style="color:rgb(0,0,0)">Function</span><<span style="color:rgb(0,0,0)">StructuredTaskScope</span>.<span style="color:rgb(0,0,0)">Subtask</span><? <span style="color:rgb(0,51,179)">extends </span><span style="color:rgb(0,126,138)">T</span>>, <span style="color:rgb(0,0,0)">Boolean</span>> <span style="color:rgb(135,16,148)">trigger</span>;<br><br> <span style="color:rgb(0,98,122)">TriggerScope</span>(<span style="color:rgb(0,0,0)">Function</span><<span style="color:rgb(0,0,0)">StructuredTaskScope</span>.<span style="color:rgb(0,0,0)">Subtask</span><? <span style="color:rgb(0,51,179)">extends </span><span style="color:rgb(0,126,138)">T</span>>, <span style="color:rgb(0,0,0)">Boolean</span>> trigger) {<br> <span style="color:rgb(0,51,179)">this</span>.<span style="color:rgb(135,16,148)">trigger </span>= trigger;<br> }<br><br> <span style="color:rgb(158,136,13)">@Override<br></span><span style="color:rgb(158,136,13)"> </span><span style="color:rgb(0,51,179)">protected void </span><span style="color:rgb(0,98,122)">handleComplete</span>(<span style="color:rgb(0,0,0)">Subtask</span><? <span style="color:rgb(0,51,179)">extends </span><span style="color:rgb(0,126,138)">T</span>> subtask) {<br> <span style="color:rgb(0,51,179)">if </span>(<span style="color:rgb(135,16,148)">trigger</span>.apply(subtask)) {<br> shutdown();<br> }<br> }<br>}</pre>The above scope allows me to implement a new scope like below:<pre><pre style="color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">public static void </span><span style="color:rgb(0,98,122)">main</span>(<span style="color:rgb(0,0,0)">String</span>[] args) <span style="color:rgb(0,51,179)">throws </span><span style="color:rgb(0,0,0)">InterruptedException </span>{<br><br> <span style="color:rgb(0,0,0)">Main main </span>= <span style="color:rgb(0,51,179)">new </span>Main();<br><br> <span style="color:rgb(0,51,179)">try </span>(<span style="color:rgb(0,51,179)">var </span><span style="color:rgb(0,0,0)">scope </span>= <span style="color:rgb(0,51,179)">new </span>TriggerScope<<span style="color:rgb(0,0,0)">Product</span>>(<span style="color:rgb(0,0,0)">main</span>::trigger)) {<br><br> <span style="color:rgb(0,0,0)">StructuredTaskScope</span>.<span style="color:rgb(0,0,0)">Subtask</span><<span style="color:rgb(0,0,0)">Product</span>> <span style="color:rgb(0,0,0)">fork </span>= <span style="color:rgb(0,0,0)">scope</span>.fork(() -> <span style="color:rgb(0,51,179)">new </span>Product(<span style="color:rgb(23,80,235)">50</span>));<br> <span style="color:rgb(0,0,0)">StructuredTaskScope</span>.<span style="color:rgb(0,0,0)">Subtask</span><<span style="color:rgb(0,0,0)">Product</span>> <span style="color:rgb(0,0,0)">fork1 </span>= <span style="color:rgb(0,0,0)">scope</span>.fork(() -> <span style="color:rgb(0,51,179)">new </span>Product(<span style="color:rgb(23,80,235)">100</span>));<br> <span style="color:rgb(0,0,0)">StructuredTaskScope</span>.<span style="color:rgb(0,0,0)">Subtask</span><<span style="color:rgb(0,0,0)">Product</span>> <span style="color:rgb(0,0,0)">fork2 </span>= <span style="color:rgb(0,0,0)">scope</span>.fork(() -> <span style="color:rgb(0,51,179)">new </span>Product(<span style="color:rgb(23,80,235)">150</span>));<br><br> <span style="color:rgb(0,0,0)">scope</span>.join();<br> }<br><br>}<br><br><span style="color:rgb(0,51,179)">private boolean </span><span style="color:rgb(0,98,122)">trigger</span>(<span style="color:rgb(0,0,0)">StructuredTaskScope</span>.<span style="color:rgb(0,0,0)">Subtask</span><? <span style="color:rgb(0,51,179)">extends </span><span style="color:rgb(0,0,0)">Product</span>> subtask) {<br> <span style="color:rgb(0,51,179)">if </span>(<span style="color:rgb(0,0,0)">Subtask</span>.<span style="color:rgb(0,0,0)">State</span>.<span style="color:rgb(135,16,148);font-style:italic">SUCCESS</span>.equals(subtask.state()) && subtask.get().price() > <span style="color:rgb(23,80,235)">50 </span>&& subtask.get().price() < <span style="color:rgb(23,80,235)">150</span>){<br> <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"result: " </span>+ subtask.get().price());<br> <span style="color:rgb(0,51,179)">return true</span>;<br> }<br> <span style="color:rgb(0,51,179)">return false</span>;<br>}</pre><pre style="color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><br></pre><font face="arial, sans-serif">In my opinion, this way is more convenient, but is this still a correct implementation or does this deviate too much from the original idea of creating a scope in its own class?<br><br>Looking forward to hearing your thoughts.<br><br>Kind regards,<br>David</font></pre></div></div></div>
</div></blockquote></body></html>