java-Threadpool Executor完成后如何给出消息?

当我的ThreadpoolExecutor执行完毕时,我试图给出一个弹出警报消息.它正在从网站搜索电子邮件地址,完成后,我希望收到一条警报消息“已完成”.这是我的主题:

public class Constant
    {
      public  static final int NUM_OF_THREAD = 60;
      public  static final int TIME_OUT = 10000;
    }
    ThreadPoolExecutor poolMainExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool
            (Constant.NUM_OF_THREAD);

这是我的搜索操作类:-

class SearchingOperation implements Runnable {

        URL urldata;
        int i;
        Set<String> emailAddresses;
        int level;

        SearchingOperation(URL urldata, int i, Set<String> emailAddresses, int level) {
            this.urldata = urldata;
            this.i = i;
            this.emailAddresses = emailAddresses;
            this.level = level;
            if (level != 1)
                model.setValueAt(urldata.getProtocol() + "://" + urldata.getHost() + "/contacts", i, 3);

        }

        public void run() {
            BufferedReader bufferreader1 = null;
            InputStreamReader emailReader = null;
            System.out.println(this.i + ":" + poolMainExecutor.getActiveCount() + ":" + level + ";" + urldata.toString());

            try {
                if (level < 1) {
                    String httpPatternString = "https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)";
                    String httpString = "";
                    BufferedReader bufferreaderHTTP = null;
                    InputStreamReader httpReader = null;
                    try {

                        httpReader = new InputStreamReader(urldata.openStream());
                        bufferreaderHTTP = new BufferedReader(httpReader
                        );
                        StringBuilder rawhttp = new StringBuilder();
                        while ((httpString = bufferreaderHTTP.readLine()) != null) {

                            rawhttp.append(httpString);

                        }
                        if (rawhttp.toString().isEmpty()) {
                            return;
                        }
                        List<String> urls = getURL(rawhttp.toString());
                        for (String url : urls) {
                            String fullUrl = getMatchRegex(url, httpPatternString);
                            if (fullUrl.isEmpty()) {
                                if (!url.startsWith("/")) {
                                    url = "/" + url;
                                }

                                String address = urldata.getProtocol() + "://" + urldata.getHost() + url;
                                fullUrl = getMatchRegex(address, httpPatternString);

                            }
                            if (!addressWorked.contains(fullUrl) && fullUrl.contains(urldata.getHost())) {
                                addressWorked.add(fullUrl);
                                sendToSearch(fullUrl);

                            }
                        }


                    } catch (Exception e) {
                        //System.out.println("652" + e.getMessage());
                        //e.printStackTrace();
                        return;
                    } finally {
                        try {
                            if (httpReader != null)
                                bufferreaderHTTP.close();
                        } catch (Exception e) {
                            // e.printStackTrace();
                        }
                        try {
                            if (httpReader != null)
                                httpReader.close();
                        } catch (Exception e) {
                            e.printStackTrace();

                        }

                    }
                }
                String someString = "";
                emailReader = new InputStreamReader(urldata.openStream());
                bufferreader1 = new BufferedReader(
                        emailReader);
                StringBuilder emailRaw = new StringBuilder();
                while ((someString = bufferreader1.readLine()) != null) {
                    if (someString.contains("@")) {
                        emailRaw.append(someString).append(";");
                    }
                }
                //Set<String> emailAddresses = new HashSet<String>();
                String emailAddress;
                //Pattern pattern = Pattern
                //.compile("\\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z0-9.-]+\\b");
                Pattern
                        pattern = Pattern
                        .compile("\\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z0-9.-]+\\b");

                Matcher matchs = pattern.matcher(emailRaw);
                while (matchs.find()) {
                    emailAddress = (emailRaw.substring(matchs.start(),
                            matchs.end()));
                    //  //System.out.println(emailAddress);
                    if (!emailAddresses.contains(emailAddress)) {
                        emailAddresses.add(emailAddress);
                        //  //System.out.println(emailAddress);
                        if (!foundItem.get(i)) {
                            table.setValueAt("Found", i, 4);
                            foundItem.set(i, true);
                        }

                        String emails = !emailAddresses.isEmpty() ? emailAddresses.toString() : "";
                        model.setValueAt(emails, i, 2);
                        model.setValueAt("", i, 3);
                    }
                }
            } catch (Exception e) {
                //System.out.println("687" + e.getMessage());
            } finally {
                try {
                    if (bufferreader1 != null)
                        bufferreader1.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (emailReader != null)
                        emailReader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Thread.currentThread().interrupt();
                return;
            }
        }

在这之后的最后一个片段:

 private void sendToSearch(String address) throws Throwable {
            SearchingOperation operation = new SearchingOperation(new URL(address), i,
                    emailAddresses, level + 1);
            //operation.run();
            try {
                final Future handler = poolMainExecutor.submit(operation);

                try {
                    handler.get(Constant.TIME_OUT, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    e.printStackTrace();
                    handler.cancel(false);
                }
            } catch (Exception e) {
                //System.out.println("Time out for:" + address);
            } catch (Error error) {
                //System.out.println("Time out for:" + address);

            } finally {
            }
        }

解决方法:

可以使用此代码.它将每隔2.5秒检查一次执行是否完成.

do {
        System.out.println("In Progress");
        try {
            Thread.sleep(2500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (poolMainExecutor.getActiveCount() != 0);

    System.out.println("Completed");
上一篇:并发7-线程池


下一篇:java – ExecutorService awaitTermination方法未按预期执行