Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give message when Threadpool Executor is completed?

I am trying to give a pop up alert message when my ThreadpoolExecutor is finished executing. It is searching email addresses from websites, once it is done I want a alert message as "Completed". Here is my Thread :-

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);

Here is my Searching Operation class :-

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;
            }
        }

After this the final snippet :-

 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 {
            }
        }
like image 985
Salman Shaikh Avatar asked May 13 '26 17:05

Salman Shaikh


1 Answers

Implement Callable<Void> instead of Runnable and wait for all the task to terminate by calling Future<Void>.get():

class SearchingOperation implements Callable<Void>
{
   public Void call() throws Exception
   {
      //same code as in run()
   }
}

//submit and wait until the task complete
Future<Void> future = poolMainExecutor.submit(new SearchingOperation()).get();
like image 165
ortis Avatar answered May 16 '26 06:05

ortis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!