Ajak6
Ajak6

Reputation: 737

Does the following situation needs synchronization?

I have a thread which enables and disables a button in certain random time, if the button clicked when it is enabled an action performed will be executed which will change the image of the button. I am concerned about the synchronization here. Suppose the button is about to get disable and got clicked, so now both threads will execute one to disable it and other to change the image. How should I synchronize this?

Upvotes: 1

Views: 96

Answers (1)

fdreger
fdreger

Reputation: 12495

All Java GUI toolkits (be it Swing, Apache Pivot, JavaFX, AWT, SWT, Android...) are single-threaded. This means that all listeners will always fire in the same thread. So:

  • no, you don't need to perform any kind of synchronization,
  • yes, you need to take care so that disabling and enabling the button happens in the gui thread (wahetever it's called). The exact code is toolkit-specific.

Upvotes: 2

Related Questions