开发者

Implementing Thread in Java from a C# background

开发者 https://www.devze.com 2023-02-07 02:42 出处:网络
I\'m trying to implement multithreading in my Java GUI application to free up the interface when a couple of intensive methods are run. I\'m primarily from a C# development background and have used Th

I'm trying to implement multithreading in my Java GUI application to free up the interface when a couple of intensive methods are run. I'm primarily from a C# development background and have used Threads in that environment a couple of times, not having much difficulty of it all really.

Roughly:

C#

Now onto the Java app itself, it's a GUI application that has a few buttons that perform differrent actions, the application plays MIDI notes using the MIDI API and I have functions such as play, stop and adding individual notes. (A key thing to note is that I do not play MIDI files but manually create the notes/messages, playing them through a track).

There are three particular operations I want to run in their own thread

  1. Play stored MIDI notes
  2. Display a list of instruments via a text box
  3. Generate 100 random notes

I have a class called MIDIControl that contains all of the functionality necessary such as the actual operations to play,stop and generate the messages I need. There is an instance of this object created in the FooView.Java class for the GUI form itself, this means for example:

  1. Press "Generate"
  2. Event handler performs the "GenerateNotes" method in the FooView.Java class
  3. This method then performs the "Generate" method in the MIDIControl instance

I've looked at implementing threads through Java and from what I've seen it's done in a different manner to the C# method, can anybody explain to me how I could implement threads in my situation?

I can provide code samples if necessary, thanks for your time.


Java threads are created the same way as C# threads, except that you pass the thread a Runnable implementation instead of a delegate. (Because Java doesn't support delegates)


Java Concurrency in Practice is your guide. Pls also have a look at SwingWorker. Remember that all UI related changes (either component model or its properties) should always be done on Event Dispatch Thread.


Background tasks in Java GUI applications are often done using the SwingWorker class, which is designed specifically for that purpose.


You'll need to distinguish between tasks that update the GUI, and tasks that do not.

If your task needs to update GUI elements, such as your task (2), you'll need to sub-class SwingWorker. The processing code (calls to your exising library) go in your override of doInBackground(), sending out any data through publish(). Then, your SwingWorker process() override can interact with your Swing components.

The reason: Swing is not thread-safe, so it will potentially break if accessed from threads other than the Event Dispatch Thread (EDT). process() will run in the EDT.

For tasks that don't update the GUI, create a new class which implements Runnable and insert the appropriate MIDI library code call in the run() method. You may then pass this as a target to a new thread as in new Thread(myRunnable).start().


As others have said it's the SwingWorker class you're after, this will enable a swing component to fire off a task in another thread and be notified of its completion and progress in a thread safe manner. You can't just spout off random threads using the raw thread runnable objects and then expect to interact with swing through those threads; swing is not thread safe by design so by doing things this way you'll almost certainly introduce subtle threading bugs into your code.

Depending on what version of Java you're using you can either download the SwingWorker separately or use the one built into the API.

If you're using Java 6 (or above) then swing worker is in the core API here.

If you're using Java 5 then the Java 6 version has been backported here.

If you're using an earlier version then you'll have to add sun's original version in which is here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号