实际上有一种方法可以在主线程上以编程方式运行 SWT 的readAndDispatch,而无需使用命令行选项。它一般不常被提及,在谈论 SWT 时甚至更少提及。我刚刚发现了这一点,它可以挽救生命。
com.apple.concurrent.Dispatch 仅存在于 Mac 上,并且可以生成一个Executor,它允许将任务提交到主线程。使用它可以创建一个始终在双击时打开的跨平台 SWT 应用程序。
当然,因为 Windows/Linux 上不存在该类,所以不能简单地直接调用所需的方法。如果您这样做,您的应用程序将在 Windows/Linux 上从ClassNotFoundException 失败。相反,仅当当前操作系统是 Mac 时,您才需要使用一些反射在主线程上运行 readAndDispatch。
您需要检查操作系统是否为 Mac。为此,您可以使用以下代码。
if (System.getProperty("os.name").toLowerCase().contains("mac"))
之后,就可以使用反射来调用必要的方法了。
Executor executor;
try {
Class<?> dispatchClass = Class.forName("com.apple.concurrent.Dispatch");
Object dispatchInstance = dispatchClass.getMethod("getInstance").invoke(null);
executor = (Executor) dispatchClass.getMethod("getNonBlockingMainQueueExecutor").invoke(dispatchInstance);
} catch (Throwable throwable) {
throw new RuntimeException("Could not reflectively access Dispatch", throwable);
}
最后,您可以在 executor 中执行您的 runnable,并为您的应用程序再次跨平台而哭泣。