rspec/rules/S3035/java/rule.adoc

57 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public static void main(String args[]) {
makeGui(); // Noncompliant
}
public void makeGui() {
JFrame frame = new JFrame();
// ...
frame.show();
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
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[]