简体   繁体   中英

How to pause a thread using wait and notify JavaFX

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
            }


        });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM