线程在javafx中正确运行在java中不正确

时间:2017-09-18 19:19:47

标签: java multithreading javafx

有人可以向我解释为什么当我使用带有gui的javafx(例如一个按钮)时我的应用程序正确运行,以及为什么它在没有gui的情况下运行相同的方法时却没有? 这是一个例子:

JavaFX的

简单的控制器:

    public class ParserGuiController implements Initializable{

    @FXML
    private Button startButton;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        startButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                try {
                    Parser.parseText();
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}

parseText()方法

public static void parseText() throws IOException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i = 1 ; i <= 2 ; i++){
            executorService.execute(new GetLinkThread(i));// GetLinkThread is a simply class that extends Task<Void> and have call method with getLink() method
        }
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.HOURS);

        ExecutorService executorService2 = Executors.newCachedThreadPool();
        for(String url : Parser.linkSet){
            executorService2.execute(new GetTextThread(url));// GetTextThread is a simply class that extends Task<Void> and have call method with getText() method
        }
        executorService2.shutdown();
        executorService2.awaitTermination(1, TimeUnit.HOURS);
        Parser.setIsParseEnd(true);//set value when is true program start run javafx window with map

    }

当我使用javaFx并且我有一个GUI时它运行正常。 当我在main parseText();类中尝试运行方法Parser时,只运行此executorService2.execute(new GetTextThread(url));其余代码无法运行。

爪哇

在Parser课程中

public static void main(String[] args) throws IOException, InterruptedException {
        parseText();
    }

为什么会发生这种情况?如何在没有GUI的情况下运行它?

0 个答案:

没有答案