带线程的Java程序不会退出

时间:2018-11-12 18:51:56

标签: java linux multithreading debugging terminal

我正在Linux Mint 19上使用openjdk 1.8编写Java程序。

我的程序产生一个带有可运行线程。

当我从命令行运行该程序时,该程序在完成后再也不会退出到命令提示符。

但是在Visual Studio代码中,当我使用调试器时,它确实可以正确退出。 当我连接调试器并逐步执行代码时,在run()方法完成后,将调用线程exit()方法,然后程序退出。

但是从Linux Mint的终端上,该程序永远不会退出。我很难确定是什么原因造成的,因为当我调试它时,它总是可以工作。

我正在使用的Microsoft VSCode java调试器扩展已配置并确认指向与在命令行环境中配置的相同的jdk版本。

vscode java调试器环境和我的命令行环境之间可能导致这种差异的原因。

这是产生线程的代码...

public void play(){

mState = State.STARTING;

thread = new Thread()
{
    public void run()
    {
        mPlayer.open();
        boolean started = false;

        mState = BeatMachine.State.STARTED;

        boolean wasNextBufferPlayed = true;
        int end = 0;

        while (mState == BeatMachine.State.STARTED && !areAllTracksComplete()) {

            if (wasNextBufferPlayed) {
                // calculate how much audio we can write and make sure it is an even frame (end % channels == 0)
                end = mPlayer.availableFrames() * mPlayer.getChannels();
                wasNextBufferPlayed = false;

                for (int i = 0; i < mTracks.size(); i++) {
                    // get the next buffer of audio from the current track
                    mTracks.get(i).nextBuffer(mMixerBuffer , end);

                    // if it is the first track, overwrite the buffer,
                    // otherwise mix the buffer
                    for (int j = 0; j < end; j++) {
                        if (i == 0) // maybe this if could be optimized out of this loop
                            mMasterBuffer[j] = mMixerBuffer[j];
                        else
                            mMasterBuffer[j] += mMixerBuffer[j];
                    }
                    wasNextBufferPlayed = false;
                }
            }

            wasNextBufferPlayed = mPlayer.play(mMasterBuffer , end);

            // the buffer is completely full (could FloatAudioPlayer potentially have autostart behavior)
            if (!started && mPlayer.availableFrames() == 0) {
                mPlayer.start();
                started = true;
            }
        }
        mPlayer.close();
        mState = BeatMachine.State.STOPPED; 
        System.out.println("This line prints in all scenarios");
        }
    };

    thread.start();
}

0 个答案:

没有答案