Runnable不执行应该执行的操作

时间:2017-05-25 13:10:40

标签: java multithreading

我有TIMER_funk - 倒数为0的方法

我有弹出新窗口的方法

public void DialogWindowkk(String Title, String Header, String Content)  {
    Alert alert = new Alert(AlertType.WARNING);
    alert.setTitle(Title);
    alert.setHeaderText(Header);
    alert.setContentText(Content);
    Dialog dialog = new Dialog();
    Stage stage = new Stage();
    stage = (Stage) dialog.getDialogPane().getScene().getWindow();
    alert.showAndWait();};

我需要当第一个计时器开始工作时,然后第一个窗口弹出(task3)。当第二个计时器开始工作时,弹出一个新窗口(task4)。但现在只适用于计时器。 Windows不会弹出。方法有效。

Runnable task1 = () -> {TIMER_funk(task11);};
Runnable task2 = () -> {TIMER_funk(task22);};
Runnable task3 = () -> {DialogWindowkk("WOW you cool");};
Runnable task4 = () -> {DialogWindowkk("HAHAHAp");};

ExecutorService service = Executors.newSingleThreadExecutor();
 for(int i=1; i<11; i++){

    service.submit(task3);

    service.submit(() -> {
    Thread.sleep(1000);
    рр.cancel();
    return null;});

    service.submit(task1);

    service.submit(() -> {
    Thread.sleep(sleep1);
    рр.cancel();
    return null;});

    service.submit(task4);

    service.submit(() -> {
    Thread.sleep(1000);
    рр.cancel();
    return null;});

    service.submit(task2);

    service.submit(() -> {
    Thread.sleep(sleep2);
    рр.cancel();return null;});  }

1 个答案:

答案 0 :(得分:1)

将对话框任务提交到事件队列。例如:

EventQueue.invokeLater(task3);Platform.runLater(task3);代替service.submit(task3);