1
votes

I am unclear on how to use wait() and notify() to pause a thread. I read talk about synchronizing, but I'm unsure how to do it in my instance. I have a music player with a progress bar where I want to pause the thread in control of syncing the progress bar with the music. Here is the thread I want to pause:

@FXML private void clickedButton(ActionEvent event){
        shuffle.setOnAction(e -> {


            artistPane.setText(model.getCurrentSong());


                if(firstTime){
                    //Multithreading with JavaFX. Essentially this other thread will check the slider to make sure its on track.
                    sliderThread = new Task<Void>() {

                        @Override
                        protected Void call() throws Exception {
                            boolean fxApplicationThread = Platform.isFxApplicationThread();
                            System.out.println("Is call on FXApplicationThread: " + fxApplicationThread);


                            //this is an infinite loop because now I only need to make this thread once, pausing and starting it, as opposed to making many threads
                            for(;;){
                                Thread.sleep(100);
                                progressBar.setValue(controller.getPercentageDone());

                            }


                        }

                    };

                    new Thread(sliderThread).start(); 
                    firstTime = false;
                }else if(!model.getIsPlaying()){

                    //I want to start the thread here

                }

                controller.shuffle(); //this will start the music on the next song
        });

Here is the second half where I also want to pause and start the thread:

play.setOnAction(e -> {

            controller.play(); //this will pause/start the music

            if(!model.getIsPlaying()){
                //where I want to pause the thread.
            }else{
                //I want to start the thread here
            }


        });
1

1 Answers

0
votes

I will try to give you simple example and you try to apply it to your program...

    public class TestClass extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Thread playThread ;

    TestClass() {

         playThread = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("DO SOME THING HERE");
                System.out.println("SONG WILL PLAY.....");


            }
        });
    }

    public void startMyPlayer() {
        System.out.println("PLAYING NOW...");
        playThread.start();
    }

    public void pauseMyPlayer() throws InterruptedException {
        System.out.println("PAUSED NOW...");
        playThread.wait();
    }

    public void resumeMyPlayer() {
        System.out.println("RESUMING NOW...");
        playThread.notify();
    }
}

That's it.I hope this help you.