Thread.sleep()的意外行为 [英] Unexpected behaviour with Thread.sleep()

查看:128
本文介绍了Thread.sleep()的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个播放歌曲的简单程序。

I have constructed a simple program to play a song.

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;
import javax.swing.*;
import java.awt.*;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.application.Platform;
import javafx.scene.Group; 

public class Fade {
    private void initAndShowGUI() {
        // This method is invoked on Swing thread
        JFrame frame = new JFrame("FX");
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setVisible(true);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
        });
    }

    private void initFX(JFXPanel fxPanel) {
    // This method is invoked on JavaFX thread
        Group root = new Group(); 
        Scene scene = new Scene(root, 500, 500, true);
        fxPanel.setScene(scene);

        try {
            String bip = new File("./Sound/Welcome To The Show - L1-5.mp3").toURI().toString();
            Media hit = new Media(bip);
            MediaPlayer mediaPlayer = new MediaPlayer(hit);
            mediaPlayer.play();

            Thread.sleep(10000);
            System.out.println("Fade");
        } catch (Exception e) {
            System.out.println("Exception");
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Fade().initAndShowGUI();
            }
        });
    }
}

但是当这段代码运行时, Thread.sleep(10000)在歌曲开始播放之前运行而不是在10秒后开始播放的歌曲淡出打印到安慰。我该如何解决这个问题?

However when this code is run the Thread.sleep(10000) runs before the song starts playing instead of the song starting playing then 10 seconds later fade is printed out to the console. How may I fix this?

推荐答案

您正在阻止JavaFX应用程序线程,因此它无法正常工作(可能包括处理媒体播放器的指令以启动)直到您的方法完成。你永远不应该阻止这个线程,因为它可以保证UI没有响应。

You are blocking the JavaFX Application Thread, so it cannot do its normal work (which presumably includes processing the media player's instruction to start) until your method completes. You should never block this thread, as it is guaranteed to make the UI unresponsive.

在JavaFX中实现暂停的最简单方法是使用 PauseTransition

The simplest way to implement a pause in JavaFX is to use a PauseTransition:

MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();

// never do this:
//Thread.sleep(10000);

PauseTransition pause = new PauseTransition(Duration.millis(10000));
pause.setOnFinished(e -> {
    System.out.println("Fade");
});
pause.play();

(另外,我认为你的真实应用程序有一些理由混合swing和JavaFX。否则你应该使它成为一个纯JavaFX应用程序。)

(As an aside, I assume your real application has some reason to mix swing and JavaFX. Otherwise you should make this a pure JavaFX app.)

这篇关于Thread.sleep()的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆