You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/09/29 10:50:18 UTC

[GitHub] [netbeans] MegJayan commented on a diff in pull request #4592: Provide hints for conversion to Virtual thread executor when thread pools are used

MegJayan commented on code in PR #4592:
URL: https://github.com/apache/netbeans/pull/4592#discussion_r983386169


##########
java/java.hints/src/org/netbeans/modules/java/hints/jdk/CanUseVT.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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 org.netbeans.modules.java.hints.jdk;
+
+import org.netbeans.api.java.queries.CompilerOptionsQuery;
+import org.netbeans.modules.java.hints.errors.Utilities;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.java.hints.ConstraintVariableType;
+import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.java.hints.Hint;
+import org.netbeans.spi.java.hints.HintContext;
+import org.netbeans.spi.java.hints.TriggerPattern;
+import org.netbeans.spi.java.hints.TriggerPatterns;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author mjayan
+ */
+@NbBundle.Messages({
+    "DN_CanUseVT=Can use Virtual Threads",
+    "DESC_CanUseVT=Can use virtual threads if workload is not CPU bound or number of concurrent tasks is high",
+    "ERR_CanUseVT=Can Use Virtual Thread Executor",
+    "ERR_CanUseThreadPerTask=Can Use Executors.newThreadPerTaskExecutor"
+})
+@Hint(displayName = "#DN_CanUseVT", description = "#DESC_CanUseVT", category = "rules15",
+        minSourceVersion = "19")
+public class CanUseVT {
+
+    private static final int VT_PREVIEW_JDK_VERSION = 19;
+
+    @TriggerPatterns({
+        @TriggerPattern(value = "java.util.concurrent.Executors.newFixedThreadPool($size)", constraints = @ConstraintVariableType(variable = "$size", type = "int")),
+        @TriggerPattern(value = "java.util.concurrent.Executors.newFixedThreadPool($size, $factory)", constraints = {
+            @ConstraintVariableType(variable = "$factory", type = "java.util.concurrent.ThreadFactory"),
+            @ConstraintVariableType(variable = "$size", type = "int")}),
+        @TriggerPattern(value = "java.util.concurrent.Executors.newCachedThreadPool()"),
+        @TriggerPattern(value = "java.util.concurrent.Executors.newCachedThreadPool($factory)", constraints
+                = @ConstraintVariableType(variable = "$factory", type = "java.util.concurrent.ThreadFactory"))})
+    public static ErrorDescription compute(HintContext ctx) {
+        if (Utilities.isJDKVersionLower(VT_PREVIEW_JDK_VERSION) && !CompilerOptionsQuery.getOptions(ctx.getInfo().getFileObject()).getArguments().contains("--enable-preview")) {
+            return null;
+        }
+        if (ctx.getVariables().get("$factory") != null) {
+            return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_CanUseThreadPerTask(), new Fix[0]);
+        } else {
+            return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_CanUseVT(), new Fix[0]);
+        }

Review Comment:
   I understand your point. One of the reasons why we decided to give the hint was so that user is notified of  the support of virtual threads and he/she can take a call on whether to modify the current code to use virtual threads depending on the use case. This is why we are providing just the suggestion without rewriting the code.
   
   In the initial commit there was rewrite logic as well, but after the first round of discussion, it was decided just to go with hints and not rewrite anything as there could be multiple corner cases like Executors.newFixedThreadPool(1) [as mentioned by @lahodaj  [here](https://github.com/apache/netbeans/pull/4592#pullrequestreview-1112236691)] which probably should not be rewritten.
   This is the background. Please let me know your thoughts. If you think its best not to provide hints, I can update accordingly.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists