从C#背景实现Java中的线程(Implementing Thread in Java from a C# background)

编程入门 行业动态 更新时间:2024-10-28 14:30:31
从C#背景实现Java中的线程(Implementing Thread in Java from a C# background)

我试图在我的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 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

Play stored MIDI notes Display a list of instruments via a text box 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:

Press "Generate" Event handler performs the "GenerateNotes" method in the FooView.Java class 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线程的创建方式与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)

更多推荐

本文发布于:2023-07-16 02:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1122189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   背景   Java   background   Implementing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!