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

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

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

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.