1

I understand that in Java there are classes such as Swing worker that are meant to to be used for things that take long to process so the EDT (Event Dispatch Thread) is not blocked. But why not?

I have an application and every user action being taken should be a blocking action. So in cases like that, is there a reason why I shouldnt run on the EDT thread?

For example. If the user clicks on an item that loads and process spreadsheet. Then I dont want the user to go to the next step before the processing is done. Shouldnt I do that on the EDT to ensure the "sequence" of the events.

What am I missing?

ale
  • 6,369
  • 7
  • 55
  • 65
Snake
  • 14,228
  • 27
  • 117
  • 250

3 Answers3

2

The problem is that blocking the EDT would also block any UI updates, hence your UI might look unresponsive. You'd normally use a modal dialog in that case which makes the UI only accept further user input after the process is finished. As an example, doing that would allow you to display some indeterminate progress bar (i.e. a "working" animation) - i.e. the user doesn't get the feeling the application doesn't respond anymore.

There are other options besides a modal dialog though (e.g. using the glass pane etc.), have a look here for an example: Creating a nice "LOADING..." animation

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
2

It is considered good practice to have your UI be able to cater for user actions. In your case, for long tasks, usually a Cancel button is added so that the user can cancel the action should they deem it necessary.

You could do everything on the EDT, but I would recommend against it. Some people tend to consider a (completely) unresponsive UI as a sign that the application died.

Having some sort of UI component showing the user that a (lengthy) operation is in progress is, in my opinion, the way to go.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Thank you for the comment. Good point about the cancel but in my case no option for cancel ( db updates and query) – Snake Jun 08 '16 at 13:02
2

You would be simply contradicting very basic/common design ideas of Java GUI applications.

If you want your user to "wait", then for example, you can put up modal windows, that simply don't allow him to click any button anywhere else. Doing that ... would still allow other elements in your GUI to be updated (for example if you have some "status bar" somewhere).

In other words: yes, it is pretty easy to just do anything on the EDT, and it might be tempting as "the simple design"; but chances are, that at some point you will regret this deviation of "java standards/best practices". And if that would happen ... then you are in a position that will require massive amounts of changes to get out of it.

Finally, never forget about the wtf-code-quality-metric; and accept that misusing the EDT in this way will guarantee you many WTFs in reviews.

wtf code quality metric

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thank you. And "I love the WTF graphics". The problem that I face, if I display a modal dialog then it does not let me procced to actually start running the task as it block everything. If run the task then dsiplay the modal window then no point as task wouldve been done. Not sure how – Snake Jun 08 '16 at 13:01
  • Although keeping to best practices is generally a good course of action I'd advocate trying to understand the reasoning behind them, especially in situations where you're tempted to deviate. That way you'll be able to make better decisions and if you deviate from best practices you'll most likely have very good reasons. Thus, I consider the OP's question actually to be quite good a question. - Besides that, you definitely earn a +1, especially for the WTF-metric ;) – Thomas Jun 08 '16 at 13:02
  • @Snake I see. I think this might be worth putting up a new question - where you clearly specify that underlying problem you want to solve. – GhostCat Jun 08 '16 at 13:03
  • @Thomas You are very right; and maybe Snake will follow up and creates another question to get us there. – GhostCat Jun 08 '16 at 13:03
  • I followed up with a new question as suggested. http://stackoverflow.com/questions/37703633/dialog-with-swingworker-is-a-chicken-egg – Snake Jun 08 '16 at 13:17