Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Thread in Java from a C# background

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#

  • Create a Thread object
  • Assign it a method to start from
  • Start thread

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.

like image 663
Jamie Keeling Avatar asked Dec 09 '22 11:12

Jamie Keeling


1 Answers

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)

like image 159
SLaks Avatar answered Dec 27 '22 01:12

SLaks