Java program with thread will not exit












0














I am writing a Java program using openjdk 1.8 on Linux Mint 19.



My program spawns a thread with a runnable.



When I run the program from the command line , the program never exits back to the command prompt after completion.



In Visual Studio code though, it does properly exit when I use the debugger.
When I connect the debugger and step through the code, after the run() method completes, the thread exit() method is being called, and then the program exits.



But from the terminal on Linux Mint, the program never exits. It is hard for me to determine what is causing this, since when I debug it, it always works.



The VSCode java debugger extension by Microsoft, which I am using, is configured and confirmed to be pointed to the same jdk version as is configured on my command line environment.



What could be causing this discrepancy between the vscode java debugger environment, and my command line environment.



Here is the code that spawns the thread...



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();
}









share|improve this question
























  • Can you post the Java code?
    – user10639668
    Nov 12 at 18:53










  • More context needed: What is causing the thread exit() method to be called? Is there a GUI like a JFrame involved?
    – michaeak
    Nov 12 at 20:38










  • Thread exit is always called after the run method exits.Nothing is "forcing" exit to be called.
    – ScottF
    Nov 12 at 20:39


















0














I am writing a Java program using openjdk 1.8 on Linux Mint 19.



My program spawns a thread with a runnable.



When I run the program from the command line , the program never exits back to the command prompt after completion.



In Visual Studio code though, it does properly exit when I use the debugger.
When I connect the debugger and step through the code, after the run() method completes, the thread exit() method is being called, and then the program exits.



But from the terminal on Linux Mint, the program never exits. It is hard for me to determine what is causing this, since when I debug it, it always works.



The VSCode java debugger extension by Microsoft, which I am using, is configured and confirmed to be pointed to the same jdk version as is configured on my command line environment.



What could be causing this discrepancy between the vscode java debugger environment, and my command line environment.



Here is the code that spawns the thread...



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();
}









share|improve this question
























  • Can you post the Java code?
    – user10639668
    Nov 12 at 18:53










  • More context needed: What is causing the thread exit() method to be called? Is there a GUI like a JFrame involved?
    – michaeak
    Nov 12 at 20:38










  • Thread exit is always called after the run method exits.Nothing is "forcing" exit to be called.
    – ScottF
    Nov 12 at 20:39
















0












0








0







I am writing a Java program using openjdk 1.8 on Linux Mint 19.



My program spawns a thread with a runnable.



When I run the program from the command line , the program never exits back to the command prompt after completion.



In Visual Studio code though, it does properly exit when I use the debugger.
When I connect the debugger and step through the code, after the run() method completes, the thread exit() method is being called, and then the program exits.



But from the terminal on Linux Mint, the program never exits. It is hard for me to determine what is causing this, since when I debug it, it always works.



The VSCode java debugger extension by Microsoft, which I am using, is configured and confirmed to be pointed to the same jdk version as is configured on my command line environment.



What could be causing this discrepancy between the vscode java debugger environment, and my command line environment.



Here is the code that spawns the thread...



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();
}









share|improve this question















I am writing a Java program using openjdk 1.8 on Linux Mint 19.



My program spawns a thread with a runnable.



When I run the program from the command line , the program never exits back to the command prompt after completion.



In Visual Studio code though, it does properly exit when I use the debugger.
When I connect the debugger and step through the code, after the run() method completes, the thread exit() method is being called, and then the program exits.



But from the terminal on Linux Mint, the program never exits. It is hard for me to determine what is causing this, since when I debug it, it always works.



The VSCode java debugger extension by Microsoft, which I am using, is configured and confirmed to be pointed to the same jdk version as is configured on my command line environment.



What could be causing this discrepancy between the vscode java debugger environment, and my command line environment.



Here is the code that spawns the thread...



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();
}






java linux multithreading debugging terminal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 18:58

























asked Nov 12 at 18:51









ScottF

49431642




49431642












  • Can you post the Java code?
    – user10639668
    Nov 12 at 18:53










  • More context needed: What is causing the thread exit() method to be called? Is there a GUI like a JFrame involved?
    – michaeak
    Nov 12 at 20:38










  • Thread exit is always called after the run method exits.Nothing is "forcing" exit to be called.
    – ScottF
    Nov 12 at 20:39




















  • Can you post the Java code?
    – user10639668
    Nov 12 at 18:53










  • More context needed: What is causing the thread exit() method to be called? Is there a GUI like a JFrame involved?
    – michaeak
    Nov 12 at 20:38










  • Thread exit is always called after the run method exits.Nothing is "forcing" exit to be called.
    – ScottF
    Nov 12 at 20:39


















Can you post the Java code?
– user10639668
Nov 12 at 18:53




Can you post the Java code?
– user10639668
Nov 12 at 18:53












More context needed: What is causing the thread exit() method to be called? Is there a GUI like a JFrame involved?
– michaeak
Nov 12 at 20:38




More context needed: What is causing the thread exit() method to be called? Is there a GUI like a JFrame involved?
– michaeak
Nov 12 at 20:38












Thread exit is always called after the run method exits.Nothing is "forcing" exit to be called.
– ScottF
Nov 12 at 20:39






Thread exit is always called after the run method exits.Nothing is "forcing" exit to be called.
– ScottF
Nov 12 at 20:39



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268360%2fjava-program-with-thread-will-not-exit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268360%2fjava-program-with-thread-will-not-exit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python