Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


For a correct and safe Swing application, where should you submit tasks for UI manipulation?

  1. Directly from the main thread

  2. Using the SwingUtilities.invokeLater()

  3. By creating a new thread for each task

  4. Executing in the Swing Worker thread directly

The correct answer is: Using the SwingUtilities.invokeLater()

In order to ensure a correct and safe Swing application, you should submit tasks for UI manipulation using the SwingUtilities.invokeLater() method. This is because all UI updates must be done on the event dispatch thread, and using this method ensures that the tasks will be executed on that thread. Directly using the main thread or creating a new thread for each task can lead to race conditions and other threading issues. Similarly, executing tasks directly in the Swing Worker thread can cause issues with synchronization and lead to unexpected behavior. Therefore, the best practice is to use the SwingUtilities.invokeLater() method for all UI manipulation tasks in a Swing application.