Skip to content

Commit

Permalink
Create rule S6901: setDaemon, setPriority and getThreadGroup should n…
Browse files Browse the repository at this point in the history
…ot be invoked on virtual threads (#3592)
  • Loading branch information
github-actions[bot] authored Feb 6, 2024
1 parent c7cf309 commit 5378180
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rules/S6901/java/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "\"setDaemon\", \"setPriority\" and \"getThreadGroup\" should not be invoked on virtual threads",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"java21", "bug"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6901",
"sqKey": "S6901",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
36 changes: 36 additions & 0 deletions rules/S6901/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
== Why is this an issue?

The `Thread` class has some methods that are used to monitor and manage its execution.
With the introduction of virtual threads in Java 21, there are three of these methods that behave differently
between the standard platform threads and the virtual ones.

For virtual threads:

* `Thread.setDaemon(boolean)` will throw an `IllegalArgumentException` if `false` is passed as an argument as a virtual thread daemon status is always true.
* `Thread.setPriority(int priority)` will never change the actual priority of a virtual thread, which is always equal to `Thread.NORM_PRIORITY`
* `Thread.getThreadGroup()` will return a dummy "VirtualThreads" group that is empty and should not be used
This rule reports an issue when one of these methods is invoked on a virtual thread.

=== Code examples

==== Noncompliant code example

[source,java]
----
Thread t = Thread.ofVirtual().unstarted(()->{/* some task */});
t.setPriority(1); // Noncompliant; virtual threads' priority cannot be changed
t.setDaemon(false); // Noncompliant; will throw IllegalArgumentException
t.setDaemon(true); // Noncompliant; redundant
t.start();
var threadGroup = t.getThreadGroup(); // Noncompliant; virtual thread groups should not be used
----

== Resources

=== Documentation

* Java Documentation - https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#setDaemon(boolean)[Thread.setDaemon]
* Java Documentation - https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#setPriority(int)[Thread.setPriority]
* Java Documentation - https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#getThreadGroup()[Thread.getThreadGroup]
* Java Documentation - https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ThreadGroup.html#virtualthreadgroup[Virtual threads group]
2 changes: 2 additions & 0 deletions rules/S6901/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 5378180

Please sign in to comment.