2017-05-27 23 views
1

スレッドを一時停止するためにwait()およびnotify()を使用する方法が不明です。私は同期に関する話を読んでいますが、私は自分のインスタンスでそれをやる方法がわかりません。プログレスバーと音楽を同期させる制御をスレッドで一時停止したい音楽プレーヤーがあります。ここで私は一時停止するスレッドがある:ここでは待機を使用してスレッドを一時停止する方法JavaFXに通知する

@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 
     }); 

は、私はまた、スレッドを一時停止して開始したい後半です:

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 
      } 


     }); 

答えて

0

私はあなたに簡単な例を与えることをしようとすると、あなたがしようあなたのプログラムにそれを適用する...

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(); 
    } 
} 

それはそれです。これはあなたに役立ちます。

関連する問題