java exit用法_java – 一起使用Platform.exit()和System.exit(int)

调用System.exit(…)终止Java虚拟机.

据我了解,调用Platform.exit()只是发出JavaFX Toolkit关闭的信号,导致在FX Application线程上调用应用程序实例的stop()方法,并允许FX应用程序线程终止.这反过来导致Application.launch()返回.如果你在main(…)方法中使用通常的习语:

public static void main(String[] args) {

Application.launch(args);

}

然后一旦launch()返回,main()方法就没有任何东西可以做了,并且没有(只要没有运行非守护程序线程)应用程序以正常方式退出.在任何情况下,Platform.exit()都不会创建对System.exit(…)的调用:但是在某些情况下,它将允许JVM退出,因为它没有任何东西可以执行.

如果你调用System.exit(…),JVM基本上会立即退出.因此,例如,如果在Application.launch()之后的main(…)方法中有代码,那么在调用Platform.exit()之后执行该代码,而不是在调用System.exit之后执行(. ..).类似地,如果重写Application.stop(),则在调用Platform.exit()之后调用stop()方法,但在调用System.exit(…)之后不调用.

如果您运行非守护程序线程,Platform.exit()将不会强制关闭它们,但System.exit()将会.

以下示例应演示此:

import javafx.application.Application;

import javafx.application.Platform;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

public class ExitTest extends Application {

@Override

public void stop() {

System.out.println("Stop called");

}

@Override

public void start(Stage primaryStage) {

Button startThread = new Button("Start non-daemon thread");

startThread.setOnAction(e -> new Thread(() -> {

System.out.println("Starting thread");

try {

Object lock = new Object();

synchronized(lock) {

lock.wait();

}

} catch (InterruptedException exc) {

System.err.println("Interrupted");

Thread.currentThread().interrupt();

} finally {

System.out.println("Thread complete");

}

}).start());

Button exit = new Button("Simple Exit");

exit.setOnAction(e -> {

System.out.println("Calling Platform.exit()");

Platform.exit();

});

Button forceExit = new Button("Force exit");

forceExit.setOnAction(e -> {

System.out.println("Calling Platform.exit():");

Platform.exit();

System.out.println("Calling System.exit(0):");

System.exit(0);

});

Scene scene = new Scene(new HBox(5, startThread, exit, forceExit));

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

System.out.println("launch() complete");

}

}

通常建议您通过调用Platform.exit()来退出JavaFX应用程序,这样可以正常关闭:例如,如果您需要任何“清理”代码,可以将它放在stop()方法中, Platform.exit()将允许它被执行.如果您正在运行必须终止的后台线程,请使它们成为守护程序线程,或者通过执行程序服务执行它们,并从stop()方法关闭执行程序服务.以下是使用该技术的上述示例的修改.

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

import javafx.application.Application;

import javafx.application.Platform;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

public class ExitTest extends Application {

private final ExecutorService exec = Executors.newCachedThreadPool();

@Override

public void stop() throws InterruptedException {

System.out.println("Stop called: try to let background threads complete...");

exec.shutdown();

if (exec.awaitTermination(2, TimeUnit.SECONDS)) {

System.out.println("Background threads exited");

} else {

System.out.println("Background threads did not exit, trying to force termination (via interruption)");

exec.shutdownNow();

}

}

@Override

public void start(Stage primaryStage) {

Button startThread = new Button("Start non-daemon thread");

startThread.setOnAction(e -> {

exec.submit( () -> {

System.out.println("Starting thread");

try {

// just block indefinitely:

Object lock = new Object();

synchronized(lock) {

lock.wait();

}

} catch (InterruptedException exc) {

System.out.println("Interrupted");

Thread.currentThread().interrupt();

} finally {

System.out.println("Thread complete");

}

});

});

Button exit = new Button("Simple Exit");

exit.setOnAction(e -> {

System.out.println("Calling Platform.exit()");

Platform.exit();

});

Scene scene = new Scene(new HBox(5, startThread, exit));

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

System.out.println("launch() complete");

}

}

如果要使用Platform.exit()以便正常关闭,并且希望从System.exit(…)返回值,则以下方法应该有效.请注意,这实际上并不是一种推荐的做法:在生产代码中,您根本不应该依赖支持流程退出代码的平台.

public class App extends Application {

private static int exitCode = 0 ;

public static exit(int exitCode) {

App.exitCode = exitCode ;

Platform.exit();

}

@Override

public void start(Stage primaryStage) {

// ...

someThing.addEventHander(someEventType, e -> App.exit(42));

// ...

}

@Override

public void stop() {

// cleanup code...

}

public static void main(String[] args) {

Application.launch(args);

System.exit(exitCode);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值