java jukebox

i want to implement a jukebox in java
where a user chooses a tune from a category, then this gets sent to a play list, if tunes are waiting then it gets put to the back on queue, then gets played when the others have finished. What would be the structure of this in code?
create a tune class, then use a vector of tunes for a playlist.One thing i was wondering is how to know when a particular tune is playing and then call the next one from the playlist, would this use a thread?
please help!! please provide some example code.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dappleAuthor Commented:
Edited text of question.
u can do it like so:

have 2 threads -- player and user-interface

an intermediate class will act as a 'tune queue' -- something like an event queue... say it has a put method (for the UI to put in tunes) and a get method (for the player to get the next tune in place)... the get can block if the queue is empty and the put will return immediately.. so maybe something like (i'm using the Collection API here):

public class TuneQueue {
   private List l;

   public TuneQueue() {
      l = new LinkedList();

   public synchronized Tune get() {
      while (l.size() == 0) {
         try {
         } catch (Exception e) { }
      return (Tune) l.remove(0);

   public synchronized void put(Tune a) {
so the player thread will just keep on going... like

for (;;) {
    tune = tuneQueue.get();

and the UI thread will put in tunes whenever the user selects it... by using tuneQueue.put(whatever)...
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

dappleAuthor Commented:
would it be
return(l(0)) not tune
what would cause the player thread to exit?
sorry... i don't get the second line...
if u're asking about the
return (Tune) l.remove(0); statement, the explanation is
l.remove(0) will remove the first element of l (which is the oldest) and return it as an Object... so the cast is to cast it to Tune for the return...

nothing unfortunately... however, a jukebox is supposed to "turn off" when the jukebox is "off"ed... that'll probably come from the UI thread... u can just call System.exit(int) and all threads will be 'killed'...

however, when there are no tunes, that thread will block and wait for tunes to be added
dappleAuthor Commented:
ah i see casted
one last thing how will i know when
a tune is playing and for the next one to be played, please answer the question for the points
you have been very helpful !
well, in ur tuneplayer class u can have a boolean that records if it is playing, say

private boolean playing;

add a query function... like

public boolean isPlaying() {return playing;}

and in the method play (which plays the tune) u can do something like

public void play(Tune t) {
   playing = true;
   ..... play the tune ......
   playing = false;

no synchro is needed cos this state doesn't matter much anyway....

alternatively, if the reason for wanting to know if it's playing is to do some UI thingy u can call the UI function to start/stop the animation b4/after playing

u're most welcome :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
just realised there wuz something else... i don't really understand  "for the next one to be played"... do u mean how do u know the next song to be played izzit?

well... u can implement a query method in the TuneQueue class.... something like

public synchronized Tune nextTune() {
   return (l.size()>0) ? l.get(0) : null;

which'll return null if no tunes are in the queue or the next tune
dappleAuthor Commented:
ok understand
cheerz thanks for your help
sorry to ask u so many questions!

noprob :)
we're here to help after all!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.