我试图在我的Java GUI应用程序中实现多线程,以便在运行几个密集型方法时释放接口。 我主要来自C#开发背景,并且在该环境中多次使用线程,并没有太多的困难。
大致:
C#
创建一个线程对象 为它分配一个方法从开始 开始线程现在到Java应用程序本身,它是一个GUI应用程序,它具有几个执行不同操作的按钮,应用程序使用MIDI API播放MIDI音符,并且具有播放,停止和添加单个音符等功能。 (需要注意的一个关键是我不播放MIDI文件,但手动创建音符/信息,通过音轨播放它们)。
有三个特定的操作我想在自己的线程中运行
播放存储的MIDI音符 通过文本框显示仪器列表 生成100个随机音符我有一个名为MIDIControl的类,它包含了所有需要的功能,例如播放,停止和生成我需要的消息的实际操作。 在FooView.Java类中为GUI表单本身创建了一个此对象的实例,这意味着例如:
按“生成” 事件处理程序在FooView.Java类中执行“GenerateNotes”方法 此方法然后在MIDIControl实例中执行“生成”方法我已经看过通过Java实现线程,从我看到它以不同的方式完成了C#方法,任何人都可以向我解释如何在我的情况下实现线程?
如有必要,我可以提供代码示例,谢谢您的时间。
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 threadNow 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
Play stored MIDI notes Display a list of instruments via a text box Generate 100 random notesI 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:
Press "Generate" Event handler performs the "GenerateNotes" method in the FooView.Java class This method then performs the "Generate" method in the MIDIControl instanceI'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线程的创建方式与C#线程相同,只是您将线程传递给Runnable实现,而不是委托。 (因为Java不支持代表)
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)
更多推荐
发布评论