File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Threading basics problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Threading basics problem" Watch "Threading basics problem" New topic
Author

Threading basics problem

Kasun Liyanage
Greenhorn

Joined: Mar 09, 2011
Posts: 23

Hi!

Below i've tried to create a basic java audio player- When the user selects an audio file by clicking the button that file begins to play. But then i realized that javazoom's play() method hangs the app while playing the file so i need to put that in a new thread. So i tried to implement Runnable like this: but that gives me the error: . What i need to do here?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18116
    
  39

Kasun Liyanage wrote:What i need to do here?


Runnable is *not* a marker interface -- it has a method. If you declare that something implements Runnable, you will actually have to implement the method of the Runnable interface.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
As Sheriff Wong said, if your JavaMusicPlayerBasic class implements Runnable then it has to have a method called run(), which it does not.

But I don't think you should have your JavaMusicPlayerBasic class implementing Runnable. Correct me if I'm wrong but I don't think you want the whole dialog box running in a separate thread, you just want to play the mp3 in a separate thread so that your dialog box still responds to user input.

I would create a separate class that implements Runnable. Call it, for example, Mp3Player. Then in your jButton_browseActionPerformed() method, instead of creating an instance of Player, you would create an instance of Mp3Player and call its start() method. Calling start() creates a new thread and calls the run() method on that new thread. Move the creation of Player and the play() method from jButton_browseActionPerformed() to the run() method of Mp3Player so that the mp3 file is played on the new thread.
Kasun Liyanage
Greenhorn

Joined: Mar 09, 2011
Posts: 23

Thanks guys! Tom, that was the type of explanation i needed, thanks. I'll give feedback after trying that.
Kasun Liyanage
Greenhorn

Joined: Mar 09, 2011
Posts: 23

It's working! Thanks again, Tom.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading basics problem
 
Similar Threads
adding and manipulating elements of a CCombo
Pressing a button to open a new window
Problem With GUI of Net Beans
how will we move from one page to another within the same window in Swing?
Networked Programs that work across countries