Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:13:18+00:00 2026-05-24T05:13:18+00:00

The code import javax.sound.sampled.*; import java.io.*; public class Tester { static Thread th; public

  • 0

The code

import javax.sound.sampled.*;
import java.io.*;

public class Tester {
static Thread th;


public static void main(String[] args) {
    startNewThread();   
   while( th.isAlive() == true) {
       System.out.println("sound thread is working");
   }
}

public static void startNewThread() {
   Runnable r = new Runnable() {
       public void run() {
           startPlaying();
       }
   };
   th =new Thread(r);
   th.start();
} 
public static void startPlaying() {
   try {            
        AudioInputStream ais = AudioSystem.getAudioInputStream(new File("d:/UnderTest/wavtester.wav"));
        Clip clip = AudioSystem.getClip();
        clip.open(ais);
        clip.loop(-1); // keep playing the sound                  
   } catch(Exception exc) {
       System.out.println(exc);
     }       
 }
}

This code does give the output sound thread working , but does not play anything. In this code i have started a separate thread for playing sound and the program should not terminate till the sound thread has finished it’s job .But the program terminates after printing series of sound thread working.

What is the reason for this ( for the program terminating and the sound not playing) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:13:19+00:00Added an answer on May 24, 2026 at 5:13 am

    The problem is that Clip already launches a background daemon thread to play the wave file.

    So, the execution flow of your code is as follows:

    • the main thread launches a secondary (useless) thread
    • the secondary thread launches a daemon thread (which will play the sound)
    • in the mean time, the main thread keeps printing something while the secondary thread is alive
    • When the secondary thread finishes launching the playback thread, it will end, so the secondary thread will not be in the active state anymore
    • the main thread will notice that the secondary thread is not active and will end as well
    • Since the playback thread is a daemon thread, the JVM will exit (because the only threads left are daemon threads)

    The final result is precisely what you see: some text get printed by the main thread while the secondary thread is launching the playback thread, and when the playback thread would start playing, boom, the JVM finishes. Sometimes you might even be able to listen some “clicking” from the headphones (as the sound starts to play) before the JVM exits.

    The easiest fix is to make the secondary thread (ie, a non-daemon thread) sleep while the sound is playing.

    ...
      clip.open(ais);
      clip.loop(-1);
      Thread.sleep(amountToSleep);
    ...
    

    One important thing to notice: about 1 year ago, when I worked with that java API, I noticed that the method getMicrosecondLength() is buggy. I was coding both in Windows and Linux, and in one platform I’d get the correct value, but in the other, the very same method would return the length in milliseconds!

    I found that the most reliable way to get the real length of the sound is to use the getFrameLength() method, and calculate the length from that.

    I couldn’t locate the code I wrote back then in this notebook. I will check later in another PC, and if I find it I will post a complete example (that works reliably on both Windows with Sun JVM and Linux with either OpenJDK or Sun).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java code: import javax.swing.Timer; class Main { public static void main(String args[]) { MyListener
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Code : import javax.swing.*; import java.awt.*; public class firstGUI extends JPanel { public static
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I have a sample code : import java.awt.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class
Here is my code: import java.io.*; import java.util.*; import javax.comm.*; public class SMS {
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
This is the code : import java.awt.*; import javax.swing.*; class tester { JFrame fr;
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.