【问题标题】:Java Future.isDone returning true, even though it shouldn't, which halts program progressJava Future.isDone 返回 true,即使它不应该,这会停止程序进度
【发布时间】:2023-12-14 10:29:02
【问题描述】:

我有一个 SwingWorker,它启动一个 LinkedBlockingQueue,将它传递给另一个方法(PortalDriver,下面),然后在 doInBackground() 方法中读取它。 LinkedBlockingQueue 持有自定义对象的期货(并且它肯定是正确填充的)。作为检查,我正在创建的对象(通过 ExecutorService)在构造函数的末尾有一个 println(this),所以我知道它们何时被创建。

问题是,通过 println() 调用(在 doInBackground 和构造函数中),在 doInBackground 的 while 循环中多次成功迭代后,行

System.out.println("future.isDone before get(): " + futureListing.isDone());

打印为真,即使构造函数没有打印它已被创建(这仅通过计数就清楚了)。即使创建了其余对象,while 循环也会永久阻塞。

private class ReadSchedWorker extends SwingWorker<Void, Listing> {
    private ChromeOptions options = new ChromeOptions();
    private WebDriver driver;
    private LinkedBlockingQueue<Future<Listing>> listingQueue;
    private LoginFactory loginFactory;
    private int year;
    private int month;
    private int day;

    public ReadSchedWorker(Login mediasiteLogin, Login tmsLogin,
                           int year, int month, int day) {
        System.setProperty("webdriver.chrome.driver", "\\\\private\\Home\\Desktop\\chromedriver.exe");
        listingQueue = new LinkedBlockingQueue<>();
        loginFactory = new LoginFactory(mediasiteLogin, tmsLogin);
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    protected Void doInBackground() throws Exception {
        this.options.addArguments("--disable-extensions");
        driver = new ChromeDriver(options);

        String portalUsername = loginFactory.getPortalUsername();
        String portalPassword = loginFactory.getPortalPassword();
        PortalDriver portalDriver = new PortalDriver(driver, listingQueue);
        portalDriver.getScheduleElements(portalUsername, portalPassword, year, month, day);

        while (!listingQueue.isEmpty()) {
            System.out.println("beginning");
            Future<Listing> futureListing = listingQueue.take();
            System.out.println("future.isDone before get(): " + futureListing.isDone());
            Listing listing = futureListing.get();
            System.out.println("future.isDone after get(): " + futureListing.isDone());
            System.out.println("before publish");
            publish(listing);
            System.out.println("after publish");
        }
        return null;
    }

    @Override
    protected void process(List<Listing> listings) {
        for (Listing listingItem : listings) {
            listingListModel.addElement(listingItem);
        }
    }
}

为了补全,这里是getScheduleElements,它填满了LinkedBlockingQueue:

public void getScheduleElements(String username, String password, int year, int month, int day) {
    driver.get("https://rxsecure.umaryland.edu/apps/schedules/view/?type=search&searchtype=resource&id=100&start=" +
            year + "-" + month + "-" + day + "&scope=week");
    PortalLoginPage loginPage = PageFactory.initElements(driver, PortalLoginPage.class);
    PortalScheduleEventsWeekPage scheduleEventsWeekPage = loginPage.login(username, password);
    webElementsQueue = scheduleEventsWeekPage.initEventsQueue();

    // doesn't need to be a queue...
    // this is sequential....
    // could do a parallel stream
    while (webElementsQueue.peek() != null) {
        Callable<Listing> callable = new PortalListingCallable(webElementsQueue.poll());
        Future<Listing> future = executor.submit(callable);
        listingQueue.offer(future);
    }

    executor.shutdown();
}

编辑: 我的意思的一个例子(未来没有完成,但通过 doInBackground() 中的 println()s 报告它已经完成):

beginning
future.isDone before get(): true //the future says it done, but the object being created is below
in listing contructor: PHAR580 Pharmacy Law;Specialty Pharmacy 2;Recorded in Mediasite PH S201 2015-04-27T10:00:00.000-04:00 2015-04-27T11:50:00.000-04:00

【问题讨论】:

    标签: java future executorservice


    【解决方案1】:

    打印为真,即使构造函数没有打印出它已被创建

    您很可能遇到了您没有看到的异常或错误。你必须调用 Future.get() 才能看到它。

    同时我建议你改变

    Future<Listing> future = executor.submit(callable);
    

    Future<Listing> future = executor.submit(new Callable<Listing>() {
        public Listing call() throws Throwable {
            try {
                return callable.call();
            } catch (Throwable t) {
                t.printStackTrace();
                throw t;
            }
        }
    });
    

    【讨论】:

    • 啊,你的代码允许我打印这个(第一行异常):org.openqa.selenium.remote.UnreachableBrowserException:与远程浏览器通信时出错。它可能已经死了。很高兴给你支票,因为这从技术上解决了我的问题。我不怎么使用 *,是不是这样?谢谢。
    • 您可以接受答案,如果出现更好的答案,请稍后再接受。
    • 谢谢。我想我能够弄清楚现在发生了什么。您是否建议在实践/生产中使用您的代码与我的单行代码,即Future&lt;Lising&gt; future = executor.submit(callable);
    • @douglas: AFAIK,如果只是出于这个原因,你总是应该在未来调用get()——以捕获可能发生的异常。
    • @HovercraftFullOfEels 我很困惑;我打电话给get:Listing listing = futureListing.get();是我没有捕捉到异常吗? (我是新手。这是一个真诚的问题)。