【问题标题】:how to execute a java function in parallel with a joptionpane如何与 joptionpane 并行执行 java 函数
【发布时间】:2015-09-07 08:20:19
【问题描述】:

我想在java中执行一个函数,但同时我想在操作开始和结束时向用户显示JOptionPane,问题是如果我不按下“接受”按钮第一个JOptionPane,我的功能没有启动,我希望它是自动的,我该怎么做?这是我的代码,我的函数使用的是 JRI 接口。


JOptionPane.showMessageDialog(null, "Leyendo archivos, espere un momento...","Importar archivos cel", JOptionPane.INFORMATION_MESSAGE);

REXP data = re.eval("rawdata <- read.celfiles(celFiles)");

JOptionPane.showMessageDialog(null, "Se han importado las muestras exitosamente.", "Importar archivos cel",JOptionPane.INFORMATION_MESSAGE);

【问题讨论】:

  • 在显示 JOptionPane 之前需要启动一个线程(进行计算),当显示 JOption 窗格时它将继续运行。
  • @maraca 除非“函数”被阻塞或长时间运行,它仍然会阻塞 EDT
  • As started here - “为避免出现死锁的可能性,您必须格外小心,仅创建、修改和查询 Swing 组件和模型来自事件调度线程。(添加了重点)"。 UI 的创建也被认为是对 UI 上下文的修改。没有办法知道 EDT 何时启动

标签: java swing joptionpane jri


【解决方案1】:

使用ExecutorService,我相信如果理解它应该更容易实现。 例如,

REXP data;
    try {
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        Future<REXP> submit = newCachedThreadPool.submit(new Callable<REXP>() {
            @Override
            public Object call() throws Exception {
                return re.eval("rawdata <- read.celfiles(celFiles)");
            }
        });
        data = submit.get();
    } catch (InterruptedException | ExecutionException ex) {
        System.err.println(ex.getMessage);
    }
    JOptionPane.showMessageDialog(null, "Leyendo archivos, espere un momento...", "Importar archivos cel", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null, "Se han importado las muestras exitosamente.", "Importar archivos cel", JOptionPane.INFORMATION_MESSAGE);

为方便起见,您可以使用它的其他重载方法。见documentation

【讨论】:

    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多