rspec/rules/S3035/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

57 lines
1.1 KiB
Plaintext

== Why is this an issue?
Swing interfaces should be constructed and shown from the Swing event dispatch thread. Doing so from any other thread, such as from ``++main++`` risks deadlocks since you run the risk of multiple threads accessing things which are inherently not thread-safe.
Instead, use ``++SwingUtilities.invokeLater++`` or ``++SwingUtilities.invokeAndWait++`` to kick off a ``++new Runnable++`` that handles your GUI creation.
=== Noncompliant code example
[source,java]
----
public static void main(String args[]) {
makeGui(); // Noncompliant
}
public void makeGui() {
JFrame frame = new JFrame();
// ...
frame.show();
}
----
=== Compliant solution
[source,java]
----
public static void main(String args[]) {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
makeGui();
}
}
}
public void makeGui() {
JFrame frame = new JFrame();
// ...
frame.show();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move this "xxx" call to the event dispatch thread.
endif::env-github,rspecator-view[]