gpt4 book ai didi

java - future 的结果一旦到达就会更新 UI

转载 作者:行者123 更新时间:2023-11-30 11:38:09 24 4
gpt4 key购买 nike

我有 JList 使用 DefaultListModel 更新 UI 列表中的字符串,由类返回,如图所示

class ResponseGiver implements Callable<Future>{

int i;

//Constructor to initialize i

String call(){
...............
...............
return i;
}

我还有其他类会更新从上面获得的结果

class Viewer {

ExecutorService es = new Executors.newFixedThreadPool(10);
List<Future<String>> futures = new ArrayList<Future<String>>();
for(int i =0;i<10;i++)
{
futures.add(new ResponseGiver(i));
}
for(Future<String> x : futures) //loop 2nd will be called 10 times
{
String p = x.get();
//update GUI with p
}

现在的问题是,假设在第 2 次循环中,在第 5 次循环中,get() 函数需要一些时间,比如说 10 秒,同时,其他 futures 从第 6 次到第 10 次准备好他们的结果。所以我的屏幕会等待第 5 个结果,甚至第 6 到第 10 个都准备好了。我希望在 10 个 future 中的任何一个返回结果后立即更新我的屏幕。

最佳答案

使用标准 API,您可以使用 ExecutorCompletionService .它允许提交 Callable 实例并返回 Future 对象,就像普通的 ExecutorService 一样。但它还允许按照完成的顺序获取 Future 对象:使用 ExecutorCompletionService#take()方法。此方法会阻塞,直到有新的 Future 可用。您可以将其想象为将 Futures 放入阻塞队列。

您可以启动一个线程来使用来自完成服务的这些 Future 对象。这样的 Future 的结果然后可以用于更新 GUI。 (请注意,此更新必须使用 SwingUtilities.invokeLater 在 Event Dispatch Thread 上再次完成)。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class ExecutorCompletionServiceTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().setLayout(new BorderLayout());

JButton button = new JButton("Run");
f.getContentPane().add(button, BorderLayout.NORTH);

final DefaultListModel<String> listModel = new DefaultListModel<String>();
JList<String> list = new JList<String>(listModel);
f.getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);

final Callback callback = new Callback()
{
@Override
public void call(final String result)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
listModel.addElement(result);
}
});
}
};

button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
createTasks(callback);
}
});

f.setSize(300, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
}

interface Callback
{
void call(String result);
}


private static Random random = new Random(0);

static class ResponseGiver implements Callable<String>
{
private int i;

ResponseGiver(int i)
{
this.i = i;
}

@Override
public String call()
{
int delayMS = 250 + random.nextInt(500);

// Simulate a longer delay for task 5
if (i == 5)
{
delayMS += 3000;
}
try
{
System.out.println("For "+i+" waiting "+delayMS+" ms");
Thread.sleep(delayMS);
System.out.println("For "+i+" waiting "+delayMS+" ms DONE");
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
return String.valueOf(i);
}
}

private static void createTasks(final Callback callback)
{
ExecutorService executorService = Executors.newFixedThreadPool(10);
final CompletionService<String> executorCompletionService =
new ExecutorCompletionService<String>(executorService);
final int n = 10;
for (int i = 0; i < 10; i++)
{
executorCompletionService.submit(new ResponseGiver(i));
}
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
processResults(executorCompletionService, n, callback);
}
});
thread.start();
}

private static void processResults(
CompletionService<String> completionService, int n, Callback callback)
{
for (int i = 0; i < n; i++)
{
try
{
Future<String> future = completionService.take();
String result = future.get();
if (result != null)
{
callback.call(result);
}
System.out.println("Processed "+(i+1)+" of "+n+" results");
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
}
}
}

关于java - future 的结果一旦到达就会更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13725422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com