Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止future.get()使GUI无响应以实现暂停按钮_Java_Multithreading_Interrupt_Executorservice_Future - Fatal编程技术网

Java 防止future.get()使GUI无响应以实现暂停按钮

Java 防止future.get()使GUI无响应以实现暂停按钮,java,multithreading,interrupt,executorservice,future,Java,Multithreading,Interrupt,Executorservice,Future,我正在创建一个程序,它从用户那里获取参数,进行一些复杂的计算,并将值返回给用户。由于这些计算通常需要一些时间,我希望用户能够随时暂停/恢复或停止计算。我使用多线程来运行计算,但由于我使用future.get()来检索解决方案,因此在计算完成时GUI没有响应。这是我的密码: System.out.println("Starting all threads..."); ExecutorService exec = Executors.newCachedThreadPool(); i

我正在创建一个程序,它从用户那里获取参数,进行一些复杂的计算,并将值返回给用户。由于这些计算通常需要一些时间,我希望用户能够随时暂停/恢复或停止计算。我使用多线程来运行计算,但由于我使用future.get()来检索解决方案,因此在计算完成时GUI没有响应。这是我的密码:

   System.out.println("Starting all threads...");
   ExecutorService exec = Executors.newCachedThreadPool();
   int threadCount = 4;
   int amount = 10000000;
   StringBuilder s = new StringBuilder();
   for(int i = 0; i < threadCount; i++) {
       Future<String> future = exec.submit(new Simulator(inputVariable1,inputVariable2, 
        inputVariable3, inputVariable4, inputVariable5, inputVariable6,    amount/threadCount)); 
   //No these are not the real variable names
        try {
            s.append(future.get()).append("\n");
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(StartGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
   int length = (int)(Math.log10(amount)+1);
   int start = 0;
   int totalWins = 0;
   int totalLosses = 0;
   for(int i = 0; i < threadCount; i++) {
       int pos = s.indexOf("Wins: ",start);
       int end = s.indexOf("Losses: ",start);
       totalWins += Integer.parseInt(s.substring(pos+6,end-1));
       totalLosses += Integer.parseInt(s.substring(end+8,end+7+length));
   }
   println("Total wins: " + totalWins + "\n" + "Total losses: " + totalLosses);
System.out.println(“启动所有线程…”);
ExecutorService exec=Executors.newCachedThreadPool();
int threadCount=4;
整数金额=10000000;
StringBuilder s=新的StringBuilder();
对于(int i=0;i
如您所见,我正在创建4个线程,在每个线程中运行计算,然后使用future.get()收集每个线程的结果。我遇到的问题是,我无法在GUI中实现暂停/恢复/停止按钮,因为future.get()方法使GUI在检索数据之前没有响应。我能做些什么来解决这个问题呢?

Future#在
可调用
完成之前获取
阻塞,这意味着,在
可调用
完成之前,您将阻塞事件调度线程,这意味着UI无法再响应正在生成的事件

更好的解决方案是使用
SwingWorker
,这在概念上是相同的,只是它具有向EDT提供反馈的功能,具体来说,在本例中是
done
方法。它在工作程序完成时调用,但从EDT上下文中调用,使其保存以更新UI

看一看,尤其是…

Future#get
阻塞直到
可调用的
完成,这意味着您正在阻塞事件调度线程,直到
可调用的
完成,这意味着UI无法再响应正在生成的事件

更好的解决方案是使用
SwingWorker
,这在概念上是相同的,只是它具有向EDT提供反馈的功能,具体来说,在本例中是
done
方法。它在工作程序完成时调用,但从EDT上下文中调用,使其保存以更新UI


看一看,尤其是…

对于记录,您可以测试
Future
以查看是否使用
Future.isDone()
完成。对于记录,您可以测试
Future
以查看是否使用
Future.isDone()完成
。出于某种原因,SwingWorker仍然使GUI无响应。您是从EDT中调用get吗?不调用execute,而是使用doInBackground?您希望我继续猜测还是能够更新您的问题?出于某种原因,SwingWorker仍然使GUI无响应。您正在EDT内调用get?不调用execute,而是使用doInBackground?你想让我继续猜测还是更新你的问题?