Implementing Thread in Java with C # Background

I am trying to implement multithreading in my Java GUI application to free up the interface when several intensive methods are launched. I am mainly from the background of C # development and have used Threads several times in this environment, without really experiencing any particular difficulties.

Rough:

WITH#

  • Create Thread Object
  • Assign a method starting with
  • Top of topic

Now, on the Java application itself, this is a graphical application that has several buttons that perform different actions, the application plays MIDI notes using the MIDI API, and I have features such as playing, stopping, and adding individual notes. (The main thing to note is that I do not play MIDI files, but manually create notes / messages, playing them through the track).

There are three specific operations that I want to run in my thread.

  • Play saved MIDI notes
  • Display tool list through text box
  • Create 100 random notes

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

  • Click Create
  • The event handler executes the "GenerateNotes" method in the FooView.Java class
  • This method then executes the "Generate" method on the MIDIControl instance.

I looked at the implementation of threads through Java and from what I saw differently with the C # method, can someone explain to me how I could implement threads in my situation?

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

+4
source share
5 answers

Java threads are created just like C # threads, except that you pass a Runnable thread instead of a delegate. (Since Java does not support delegates)

+6
source

Java Concurrency in Practice is your guide. Pls also has the look of SwingWorker . Remember that all changes related to the user interface (either the component model or its properties) must always be performed in the event sending thread.

+3
source

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

+2
source

You will 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 will need to subclass SwingWorker. The processing code (calls to your push library) goes into your doInBackground() override, sending any data through publish() . Overriding the SwindWorker process() can then interact with your Swing components.

Reason: Swing is not thread safe, so it will potentially break when accessed from threads other than an event sending stream (EDT). process() will work in EDT.

For tasks that do not update the GUI, create a new class that implements Runnable and inserts the corresponding MIDI library code call in the run() method. You can then pass this as a target to a new thread, as in new Thread(myRunnable).start() .

+2
source

As others have said, this is the SwingWorker class that you need, this will allow the swing component to run the task in another thread and receive notifications of its completion and progress in thread safe mode. You cannot just discard random streams using streams executed using a raw stream, and then expect interaction with the swing of these streams; swing is not thread safe by design, so by doing this you will almost certainly introduce subtle errors in the stream into your code.

Depending on which version of Java you are using, you can either download SwingWorker separately or use the built-in API.

If you are using Java 6 (or higher), then the swing worker is in the core API here .

If you are using Java 5, then a version of Java 6 has been passed here .

If you are using an earlier version, you will have to add the original version of sun, which is here .

+1
source

Source: https://habr.com/ru/post/1337208/


All Articles