gpt4 book ai didi

java - 线程新手需要java中的建议

转载 作者:行者123 更新时间:2023-12-04 05:27:54 25 4
gpt4 key购买 nike

我正在写一个音乐播放器。当我从外部音乐文件中读取数据时,我会用幅度信息填充缓冲区。因为我认为将这个阻塞操作生成到它自己的线程中是有意义的,所以我添加了一个在 runnable 中运行它的接口(interface):

public class AudioInterface implements Runnable {

public void run()
{
AudioManager am = new AudioManager();
am.play("res/sample2.mp3");
}
}

以下是我的以下问题:
  • 如何从另一个类创建一个钩子(Hook)来轮询幅度数据?我需要它来编写将绘制到 JPanel Canvas 上的算法。
  • 启用可视化的面板是否也应该在不同的线程中?
  • 当前 run() 仅包括播放文件,我将如何将此线程用于其他操作,例如摆姿势和寻找等,因为只有一个运行操作。

  • 谢谢

    最佳答案

    一些想法:

    How do I create a hook from another class to poll the amplitude data? I need it to write an algorithm that will be drawn onto a JPanel canvas.



    由于这是一个 Swing 程序,请考虑为您的后台线程使用 SwingWorker,然后通过 SwingWorker 的发布/处理方法对将幅度数据泵入程序的 GUI 部分。

    Should the panel that enables visualization also be in a different thread?



    在这里小心。所有 Swing 代码都应该在一个线程上调用,并且只能在一个线程上调用,即事件调度线程。请看一下这方面的教程, Concurrency in Swing .

    Currently run() only includes playing the file, how would I use this thread for other actions such as posing and seeking etc.



    您不为此使用“线程”,而是调用对象的方法。

    此外,您可能希望将 AudioManager 变量声明为类字段,而不是局部变量,因为否则它的范围仅限于声明它的方法,从而阻止其他代码与其交互。

    关于java - 线程新手需要java中的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993943/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com