2

After having some trouble with setting up a thread to start my MIDI sequencer I decided to simply remove it, although it would slow my UI down I would be able to use it correctly.

What I noticed however was that even when playing the Sequencer the UI was very much active, even if it was playing around 500 notes the UI worked perfectly fine.

Now I know that in C# if you are doing something intensive it is advisable to load it on a new Thread as it will free the UI. Is it the same principle in Java, it's really confused me. If so can someone explain how the UI is not being blocked?

Thanks

Edit:

The following code actually plays the Sequence

public static boolean Play() {
    if(!_sequencer.isRunning()) {
        try {
            _sequencer.setSequence(_sequence);
            _sequencer.start();

            return true;
        } catch (Exception e) {
            Logger.Add(e.getMessage());
        }
    }
    return false;
    //Already running
}

4 Answers 4

7

Yes, it is the same theory. Only the Event Thread can modify the UI, and thus if you are doing anything on that thread, then you are preventing other events from working on the UI.

It may be easier to think about the Event Thread as a queue:

  1. Show Form
  2. Click Button
  3. Do your work (Action)
  4. Reset focus of Button
  5. Update Progress Bar
  6. Et cetera

If #3 takes long, then it may mean that your form will appear locked up. Obviously it completely depends on your definition of long. In general, it's better to work off of the Event Thread rather than on it.

Sign up to request clarification or add additional context in comments.

5 Comments

It's even called the EventQueue. :-)
Good catch, I am so used to commenting EDT (Event Dispatch Thread) that I forget about that name :).
Okay, so should I assume that the Java MIDI API creates the sequencer on a seperate thread but keeps it hidden? It's a process that may continue to run for a while but seems to have no effect on the UI or any of the UI events.
@Jamie Keeling I have never used the MIDI API. From the looks of it, it would appear that it is spinning off another thread (based on the available methods). I searched for a bit and could not find anything definitive either way. This FAQ suggested that it was multithreaded.
pickypg You found the same as me then, seems that there's not a lot of details about it's implementation. I guess I'll have to assume it is on a new thread then, thanks for the feedback!
2

It's definitely the same principal. Generally speaking you want to only do minimal work with the UI thread. If it ends up taking any significant time, it can cause the UI to be unresponsive and you can get a "Not Responding" error. You want to keep the UI thread as free as possible so it can respond to user interaction.

Comments

2

If your application has a graphical user interface, it's advised that you perform expensive calculations on a new Thread to keep your graphics from freezing. You can either create a SwingWorker, or use the Callable/Future idiom.


Comments

1

Yes, you're right. Read Threads and Swing for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.