Reputation: 4289
I am new to java and there was an interview question for a Graduate role which i didn't understand, so can you tell me which thread and its purpose please.
Upvotes: 1
Views: 444
Reputation: 25954
You're looking for the Event Dispatch Thread.
Knowing about it is pretty.. important. It is vital to understand what thread your code will end up running on - long-running tasks should not be run on the EDT. Instead, dispatch those tasks on their own thread, then run a callback at the end of the operation to update the GUI using SwingUtilities
.
Upvotes: 3
Reputation: 31724
Swing UI toolkit is single threaded (in the sense that it is unsafe to call any of the swing libraries from any other thread). All the UI events, both rendering and dispatching them is done by the EDT thread
.
So on clicking at a button on desktop. The OS gives the notification to EDT which receives the event and then triggers the callback function which was registered for the button click. Moreover from the code, when you do some action (for ex: repaint screen or draw Image). The EDT
delivers the change to the OS and it is rendered on the screen. Hence a 2-way interaction.
Because as mentioned Swing UI toolkit is single-threaded. Hence it is advisable to call any of the Swing libraries from EDT only usign System.invokeLater. More info here.
Upvotes: 3