[ 
https://issues.apache.org/jira/browse/GROOVY-9381?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18033048#comment-18033048
 ] 

ASF GitHub Bot commented on GROOVY-9381:
----------------------------------------

Copilot commented on code in PR #2324:
URL: https://github.com/apache/groovy/pull/2324#discussion_r2463960413


##########
src/main/java/groovy/util/concurrent/async/AsyncHelper.java:
##########
@@ -0,0 +1,96 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.util.concurrent.async;
+
+import org.apache.groovy.util.SystemUtil;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.function.Supplier;
+
+/**
+ * Helper class for async/await operations
+ *
+ * @since 6.0.0
+ */
+public class AsyncHelper {

Review Comment:
   The system property 'groovy.async.parallelism' is not documented. Consider 
adding a comment explaining its purpose and valid values.
   ```suggestion
   public class AsyncHelper {
       /**
        * The system property "groovy.async.parallelism" controls the 
parallelism level
        * (number of threads) used for the async thread pool. Valid values are 
positive integers.
        * If not set, the default is (number of available processors + 1).
        * Setting this value too high may lead to resource exhaustion; too low 
may reduce concurrency.
        */
   ```



##########
src/main/java/groovy/util/concurrent/async/AsyncHelper.java:
##########
@@ -0,0 +1,96 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.util.concurrent.async;
+
+import org.apache.groovy.util.SystemUtil;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.function.Supplier;
+
+/**
+ * Helper class for async/await operations
+ *
+ * @since 6.0.0
+ */
+public class AsyncHelper {
+    private static final int PARALLELISM = 
SystemUtil.getIntegerSafe("groovy.async.parallelism", 
Runtime.getRuntime().availableProcessors() + 1);
+    private static final Executor DEFAULT_EXECUTOR;
+    private static int seq;

Review Comment:
   The static field `seq` is accessed from multiple threads without 
synchronization in the thread factory. Use AtomicInteger instead to prevent 
race conditions.



##########
src/antlr/GroovyLexer.g4:
##########
@@ -485,6 +487,7 @@ VOLATILE      : 'volatile';
 WHILE         : 'while';
 YIELD         : 'yield';
 
+

Review Comment:
   [nitpick] Extra blank line added at line 490. Remove this unnecessary 
whitespace to maintain consistent formatting.
   ```suggestion
   
   ```





> Support async/await like ES7
> ----------------------------
>
>                 Key: GROOVY-9381
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9381
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Daniel Sun
>            Priority: Major
>
> Here is an example to show proposed syntax and backend API(Java's 
> {{CompletableFuture}} or GPars's {{{}Promise{}}}), but I think it's better 
> for Groovy to have its own {{Promise}} to decouple with Java API because 
> async/await as a language feature should be as stable as possible.
> {{async}} will generate the {{Awaitable}} instance such as Groovy {{Promise}} 
> implementing the {{Awaitable}} interface, and {{await}} can wait for any 
> {{Awaitable}} instance to complete and unwrap it for the result. 
> {code:java}
> /**
>  * 1. An async function that simulates a network API call.
>  * The 'async' keyword implies it runs asynchronously without blocking.
>  */
> async fetchUserData(userId) {
>     println "Starting to fetch data for user ${userId}..."
>     
>     // Simulate a 1-second network delay.
>     Thread.sleep(1000) 
>     
>     println "Fetch successful!"
>     // The 'async' function implicitly returns a "CompletableFuture" or 
> "Promise" containing this value.
>     return [userId: userId, name: 'Daniel']
> }
> /**
>  * 2. An async function that uses 'await' to consume the result.
>  */
> async processUserData() {
>     println "Process started, preparing to fetch user data..."
>     
>     try {
>         // 'await' pauses this function until fetchUserData completes
>         // and returns the final result directly.
>         def user = await fetchUserData(1)
>         
>         println "Data received: ${user}"
>         return "Processing complete for ${user.name}."
>         
>     } catch (Exception e) {
>         return "An error occurred: ${e.message}"
>     }
> }
> // --- Execution ---
> println "Script starting..."
> // Kick off the entire asynchronous process.
> def future = processUserData()
> // This line executes immediately, proving the process is non-blocking.
> println "Script continues to run while user data is being fetched in the 
> background..."
> def result = future.get()
> println "Script finished: ${result}"
> {code}
> Use async/await with closure or lambda expression:
> {code}
> // use closure
> def c = async {
>     println "Process started, preparing to fetch user data..."
>     
>     try {
>         // 'await' pauses this function until fetchUserData completes
>         // and returns the final result directly.
>         def user = await fetchUserData(1)
>         
>         println "Data received: ${user}"
>         return "Processing complete for ${user.name}."
>         
>     } catch (Exception e) {
>         return "An error occurred: ${e.message}"
>     }
> }
> def future = c()
> {code}
> {code}
> // use lambda expression
> def c = async () -> {
>     println "Process started, preparing to fetch user data..."
>     
>     try {
>         // 'await' pauses this function until fetchUserData completes
>         // and returns the final result directly.
>         def user = await fetchUserData(1)
>         
>         println "Data received: ${user}"
>         return "Processing complete for ${user.name}."
>         
>     } catch (Exception e) {
>         return "An error occurred: ${e.message}"
>     }
> }
> def future = c()
>  {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to