java - 在执行另一个 SwingWorker 之前等待 SwingWorker 完成

标签 java multithreading swing concurrency swingworker

我试图在 SO 上找到我的问题的答案,但由于它们的丰富性和多样性,我有点困惑。这是我的问题:我的应用程序比较两个文件并在 Swing.JTextPane 中打印结果。我使用按钮调用处理文件的代码,为了避免挂起 UI,我使用 SwingWorker 处理每对文件。这是它的代码:

class ProcessAndPrintTask extends SwingWorker<Void, Void> {
        private Report report;
        Integer reportResult;
        ProcessAndPrintTask(Report report) {
            this.report = report;
            reportResult = null;
        }

        @Override
        protected Void doInBackground() {

            try {
                reportResult = report.getComparator().compareTwoFiles(new FileInputStream(new File(pathToReportsA + report.getFilename())),
                        new FileInputStream(new File(pathToReportsB + report.getFilename())));
            }

            catch (IOException ex) {
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void done() {

            String message = report.getFilename() + ": ";
            if (reportResult != null) {
                switch (reportResult) {
                    case 1:
                        StyleConstants.setBackground(style, Color.GREEN);
                        try {
                            doc.insertString(doc.getLength(), message + "MATCH\n", style);
                        }
                        catch (BadLocationException ex) {ex.printStackTrace();}
                        break;
                    case 0:
                        StyleConstants.setBackground(style, Color.RED);
                        try {
                            doc.insertString(doc.getLength(), message + "NO MATCH\n\n", style);
                            try {
                                for (String s : report.getComparator().getDifferences(
                                        new FileInputStream(new File(pathToReportsA + report.getFilename())),
                                        new FileInputStream(new File(pathToReportsB + report.getFilename())))) {
                                    doc.insertString(doc.getLength(), s + "\n", style);
                                }
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                        catch (BadLocationException ex) {ex.printStackTrace();}
                        break;
                    case -1:
                        StyleConstants.setBackground(style, Color.CYAN);
                        try {
                            doc.insertString(doc.getLength(), message + "BOTH FILES EMPTY\n", style);
                        }
                        catch (BadLocationException ex) {ex.printStackTrace();}
                        break;
                    default:
                        StyleConstants.setBackground(style, Color.ORANGE);
                        try {
                            doc.insertString(doc.getLength(), message + "PROBLEM\n", style);
                        }
                        catch (BadLocationException ex) {ex.printStackTrace();}

                }
            }
            else {
                StyleConstants.setBackground(style, Color.ORANGE);
                try {
                    doc.insertString(doc.getLength(), message + "FILE OR FILES NOT FOUND\n", style);
                }
                catch (BadLocationException ex) {ex.printStackTrace();}
            }

        }
    }

doInBackground() 进行比较,done() 根据比较结果格式化消息并打印它。问题在于,程序不会等到一对被处理并打印,因此结果不会按照打开的顺序打印,这可能会让用户感到非常困惑:大多数文件都很小,而且实际上很容易被打印。速度很快,因此比较似乎在某个时刻完成,但仍有更大的文件正在处理。

我读到了使用 PropertyChangeListener 的可能性,但我不明白它与使用 done() 方法有什么不同...我尝试对两者进行比较并在 doInBackground() 中打印,但这会弄乱格式(这是可以预料的 - 在打印完成之前,背景颜色已更改)。我还尝试在调用 SwingWorker 的循环内调用 Thread.sleep() 任意时间,如下所示:

try (FileInputStream reportListExcelFile = new FileInputStream(new File(reportListPath))) {
                Workbook workbook = new XSSFWorkbook(reportListExcelFile);
                Sheet sheet = workbook.getSheetAt(0);
                Iterator<Row> iter = sheet.iterator();

                // skip first row that contains columns names
                iter.next();

                while (iter.hasNext()) {
                    try {Thread.sleep(1000);} catch (Exception ex) {ex.printStackTrace();}
                    Row r = iter.next();
                    String name = r.getCell(0).getStringCellValue();
                    String format = r.getCell(1).getStringCellValue();
                    Report currentReport = new Report(name, format);
                    new ProcessAndPrintTask(currentReport).execute();
                }
            }

它不仅看起来是一个丑陋的拐杖,而且还导致 GUI 挂起,直到比较所有文件对。

有解决办法吗?

最佳答案

一旦我完成了一个OrderedResultsExecutors,它可以维护添加任务的顺序和通知结果的顺序。您所要做的就是为您的案例实现通知方法,例如。写一些Listener什么的。当然,您可以将报告集合传递给 SwingWorker 并在 for 循环中处理它们,但在这种情况下,您将失去多线程处理能力,并且所有任务可能需要相当多的时间才能在这种情况下执行单线程方式。这就是为什么最好拥有这种机制的集会多线程版本,如下所示:

Import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class OrderedResultsExecutors extends ThreadPoolExecutor {
    public OrderedResultsExecutors(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    private ConcurrentHashMap<Long, Runnable> startedTasks = new ConcurrentHashMap<>();
    private ConcurrentLinkedDeque<Runnable> finishedTasks = new ConcurrentLinkedDeque<>();
    private AtomicLong toNotify = new AtomicLong(0);
    private AtomicLong submitedCount = new AtomicLong(0);

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        startedTasks.put(submitedCount.getAndIncrement(), r);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        finishedTasks.add(r);
        finishedTask();
    }

    private void finishedTask() {
        Runnable orderedResult;
        long current;
        while ((orderedResult = startedTasks.get(current = toNotify.get())) != null
                && finishedTasks.contains(orderedResult) && (orderedResult = startedTasks.remove(current)) != null) {
            finishedTasks.remove(orderedResult);
            notify(current, orderedResult);
            toNotify.incrementAndGet();
        }
    }

    private void notify(long order, Runnable result) {
        try {
            System.out.println("order: " + order + " result: " + ((Future)result).get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    public static ExecutorService newFixedThreadPool(int noOfThreads) {
        int corePoolSize = noOfThreads;
        int maximumPoolSize = noOfThreads;
        return new OrderedResultsExecutors(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

}

关于java - 在执行另一个 SwingWorker 之前等待 SwingWorker 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45635105/

相关文章:

python - 在 python 中处理不断变化的列表的正确方法

java - 扩展 JMenu 以给它一个复选框?

java - 用于创建异步流 API 的框架

java - 屏幕旋转时 fragment 崩溃

java - MySQL 集群中的 XA 事务支持?

Ruby运行两个进程,输出结果到终端,安全结束

java - 我需要获取数组元素的数量?

c++ - 在没有多线程的情况下,我的代码更快是正常的,这是我做错了吗?

java - 如何发现Java Swing可能出现的问题?

java - 在 Swing 中没有得到预期的输出