aspose file tools*
The moose likes Beginning Java and the fly likes Why doesn't my progam quit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't my progam quit?

Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hello everybody,

I absolutely can't find the reason why my program doesn't quit.
It is one of the examples in the book 'Head First Java'.

This is 'my' code:



Everything seems to work fine, it plays the sound as expected and obviously also jumps back to main, because the terminal also shows the message which I have entered for debugging, 'System.out.println("after mini.play");' at the end of main.
But then - the program never quits and the terminal stands there waiting forever.

The problem occurs on my Ubuntu Linux computer as well as on another one with XP on it. For this reason I think that I made a mistake which I am simply not able to find.

What did I do wrong?

Cheers,

Oliver
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You need to close() the Sequencer. Without that it still has a running thread somewhere. The issue is when to close it, though. You should wait for an event that signals the playing is done. Now that's usually where ControllerEventListener is for, but my tests haven't been able to get it to really listen.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Misha van Tol
Ranch Hand

Joined: Jan 02, 2010
Posts: 56

You can use the isRunning() method on player to check when it's done.
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hi!

Thank you so much for your replies. Without them I never would have found the error.
They really should have mentioned in the book that this behaviour will occur - I double checked the code in the book, but there is definitely no close() method used.

Well - now it works, even so my solution is probably bad style!?



Btw.: while browsing through the API my first idea also was to use the isRunning() method, but that didn't work. I guess that it stays true until the sequencer's close() method is called what will never happen in this case.
That's how I used it:



Cheers,

Oliver
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

But that would only work if you would poll the sequencer from time to time. Now I did notice that a meta event is also trigger. So the code with MetaEventListener:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Oliver, isRunning() will return false before close() is called; my code proves that. It is at least a bit better because it doesn't use busy waiting.
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
hi Rob,

thanks for your help.

Rob Prime wrote:Oliver, isRunning() will return false before close() is called; my code proves that. It is at least a bit better because it doesn't use busy waiting.


I was expecting something like this, that's why I guessed that my solution is probably 'bad style'....

I don't yet exactly understand how this listener thing works, but you gave me an idea of it. Later in the book they'll also take care of listeners.

Cheers,

Oliver
 
jQuery in Action, 2nd edition
 
subject: Why doesn't my progam quit?