JavaFX application lagging when using Thread
<p>I'm having some trouble with my JavaFX application. I'm currently creating a Music Player and I have a slider at the bottom (similar to Spotify) that should move based on the duration of the song. </p>

<p>The problem is that my code lags out when using this Thread code. It gets the current duration of song and then it sets the value of the durationSlider before going to sleep for 1000 ms. </p>

<p>Does anyone know a way to fix this lag problem? The code still runs every second but my GUI lags out and I am completely unable to do anything.</p>

<pre><code>public void startCounting(){
    mediaPlayer.setOnPlaying(new Thread() {
        @Override
        public void run() {
            Duration a;
            while (mediaPlayer.getStatus().equals(MediaPlayer.Status.PLAYING)) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Shit aint workin bro");
                }
                a = mediaPlayer.getCurrentTime();
                int currentTime = (int) a.toSeconds();
                System.out.println(currentTime);
                durationSlider.setValue(50.0);  //Testing the setValue() method

            }
        }
    });
}
</code></pre>