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.


What method is used in CBox to change colors of 'stars' asynchronously?

  1. public void redraw()

  2. public void run()

  3. private void changeColor()

  4. getDisplay().asyncExec(new Runnable())

The correct answer is: getDisplay().asyncExec(new Runnable())

The getDisplay().asyncExec(new Runnable()) method is used in CBox to change colors of 'stars' asynchronously. The method allows for the execution of a code segment to be scheduled on the display thread. This is essential when making changes to the user interface as it prevents any lag or disruption to the main thread. Option A is incorrect because the redraw() method is used to update or refresh the contents of a widget on the display, not to change colors asynchronously. Option B is incorrect because the run() method is used to execute a code segment on the current thread, which is not useful for changing colors asynchronously. Option C is incorrect because the changeColor() method is marked as private, so it cannot be accessed and used outside of its own class. Therefore, the only correct and relevant option is D, using the getDisplay().asyncExec(new Runnable()) method.