Java:方法的优化每10微秒运行一次

时间:2016-01-12 08:57:14

标签: java optimization timer raspberry-pi

编辑:我对println()做了一些nanoTime(),发现stepPins[pin].toggle()需要4226微秒。现在我需要找到一种更快捷的切换方式。

我正在制作一个控制音乐软驱阵列的程序。目前我有一个方法每10微秒运行一次,并通过读取短片来打勾必要的软盘,形式为01001011,其中1 = tick floppy(有8张软盘)。

由于某些原因,代码运行速度太慢导致音符频率偏离。

以下是方法及其类:

public class Timer implements Runnable
{
    @Override
    public void run()
    {
        int i = Main.currentSteps.get(Main.time);
        try
        {
            if (Main.time > Main.maxTime)
            {
                Main.executor.remove(Main.timer);
                FloppyController.resetAll();
            }
            if ((i & 1) == 1)
            {
                FloppyController.stepPin(0);
            }
            if ((i & 2) == 2)
            {
                FloppyController.stepPin(1);
            }
            if ((i & 4) == 4)
            {
                FloppyController.stepPin(2);
            }
            if ((i & 8) == 8)
            {
                FloppyController.stepPin(3);
            }
            if ((i & 16) == 16)
            {
                FloppyController.stepPin(4);
            }
            if ((i & 32) == 32)
            {
                FloppyController.stepPin(5);
            }
            if ((i & 64) == 64)
            {
                FloppyController.stepPin(6);
            }
            if ((i & 128) == 128)
            {
                FloppyController.stepPin(7);
            }
        }
        catch (Exception e) {}
        Main.time++;
    }
}

以及它的价值,我的Main.java课程的相关部分:

public class Main
{
    public static ArrayList<Short> currentSteps;
    public static ArrayList<Note> all = new ArrayList<Note>();
    public static long maxTime = 0;
    public static ScheduledThreadPoolExecutor executor;
    public static Timer timer;

    public static int time;

    public static void main(String[] args) throws InterruptedException, InvalidMidiDataException, IOException, MidiUnavailableException
    {
        //initialize
        FloppyController.init();
        currentSteps = new ArrayList<Short>();
        FloppyController.resetAll();

        for (int o = 0; o < Math.ceil(maxTime / 10); o++)
        {
            currentSteps.add((short) 0);
        }

        //populate list
        for (Note n : all)
        {
            for (int a = Math.round(n.timeStart / 10); a <= Math.round(n.timeEnd / 10); a += n.wait)
            {
                currentSteps.set(a, (short) (currentSteps.get(a) + (((currentSteps.get(a) & (short) Math.pow(2, n.channel)) == Math.pow(2, n.channel)) ? 0 : Math.pow(2, n.channel))));
            }
        }

        //start play executions
        executor = new ScheduledThreadPoolExecutor(1);
        long resolution = 10; //# of microsecond iterations
        timer = new Timer();
        executor.scheduleAtFixedRate(timer, 0, resolution, TimeUnit.MICROSECONDS);
    }

如何优化run()的{​​{1}}方法?

我也在考虑将Timer.class ArrayList更改为HashMap,并且只包含非零值及其相应的时间作为键,因为我正在运行Raspberry Pi上的内存问题。我正在运行它。 / p>

以下是currentSteps方法:

stepPin

1 个答案:

答案 0 :(得分:0)

我建议每个软盘驱动器启动1个线程,并使用两个字节数组作为缓冲区(让我们称之为“A”和“B”)来安排声音。缓冲区应足够大,可以播放几秒钟的音乐。伪代码将是这样的:

  • 初始化:用音乐的第一秒填充A缓冲区。启动从A缓冲区读取的所有软盘线程。
  • 主线程:用下一秒音乐填充所有B缓冲区。等到软盘线程到达B缓冲区(经过一段时间)。然后写A缓冲区,依此类推,直到乐谱耗尽。
  • 软盘线程:读取当前缓冲区,使用忙等待(高清,高CPU使用)或使用sleep的“精确”版本(依赖于操作系统的定义,低CPU使用率)。通过System.nanoTime()与他人保持同步。当A缓冲区耗尽时,切换到B缓冲区。

假设stepPin正在阻塞,但只阻止其线程,这将使您的线程与这些延迟隔离。