简体   繁体   English

安排固定费率拒绝工作

[英]Schedule fixed rate refusing to work

This code produces a continious sound and works when I remove the df.calculateFreq() from the ThreadMain class. 当我从ThreadMain类中删除df.calculateFreq()时,此代码会产生连续的声音。 When I put it in the genTone() method below the console prints the "test" only once and then stops while without it the code works fine. 当我把它放在genTone()方法下面时,控制台只打印一次“test”,然后在没有它的情况下停止,代码工作正常。 Does it not have enough time to process the extra data?? 它没有足够的时间来处理额外的数据吗? Thanks and there is no errors from the code. 谢谢,代码没有错误。

public ThreadMain() {
        audio = new AudioGenerator(10000);
        audio.createPlayer();

        dF = new DetermineFreq();

        exec = Executors.newSingleThreadScheduledExecutor();
        exec.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                //RUN FUNC every 20 millisecond
                genTone();

            }
        }, 0, 50, TimeUnit.MILLISECONDS);
    }

    public void genTone(){
        System.out.println("test");
        dF.calculateFreq();   <--- this
        tone = audio.getSineWave(noteDuration, 10000, 200);
        audio.writeSound(tone);
    }

public class DetermineFreq{
    MainActivity main;

    float accelX;

    public void DetermineFreq() {
        main = new MainActivity();
    }

    public void calculateFreq() {
        accelX = main.getAccelX();
        System.out.println(accelX);
    }
}

The ScheduledExecutor may be silently throwing an error, and terminating the thread- this does happen. ScheduledExecutor可能会默默地抛出错误,并终止线程 - 这确实发生了。

Wrap the contents of your genTone() method in a try catch and print the stack trace of any exceptions caught. 在try catch中包装genTone()方法的内容,并打印捕获的任何异常的堆栈跟踪。

public void genTone(){
    try
        {
        System.out.println("test");
        dF.calculateFreq();
        tone = audio.getSineWave(noteDuration, 10000, 200);
        audio.writeSound(tone);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM