0

Hi i have a code to check a proxy. I always get false when I run the method. I understand that the problem is the last false. When I output it on the console with println, it also differs between false and true but does not return the correct one as the return value of the method. Can you help please! If the proxy is online, the code must output true

final ExecutorService es = Executors.newFixedThreadPool(100);

public boolean isProxyOnline(String proxyIp, int proxyPort) {

    es.submit(() -> {

        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort));
            URLConnection connection = new URL("http://www.google.com").openConnection(proxy);
            connection.setConnectTimeout(1000);
            connection.connect();
            System.out.println("true");
            return true;
        } catch (Exception e) {
            System.out.println("false");
            return false;
        }

    });
    return false;
}
Swipi
  • 27
  • 4

2 Answers2

0

You must return the result of es.submit, which is a Future<Boolean>, or you must wait for the result of the Future by calling get() on it, which will block.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Please have you a example with my code, I am new to lambdas – Swipi Nov 09 '21 at 17:14
  • Sure, but this has nothing to do with lambdas, really. `return es.submit(() -> { ... })`, where `...` is the whole rest of your code that was in the lambda block. – Louis Wasserman Nov 09 '21 at 17:18
  • @Swipi, The key concept here is that `es.submit(...)` does _not_ run the lambda, and it does _not_ return the value that is returned by the lambda. The lambda will be run in a _worker thread_ some time after `es.submit(...)` returns. What the submit function returns is a `Future` object that you can use later to _wait_ for the value that the lambda eventually returns. – Solomon Slow Nov 09 '21 at 18:08
0

The first way, however, there is no need to submit task to a thread pool in this way:

    public boolean isProxyOnline(String proxyIp, int proxyPort) throws ExecutionException, InterruptedException {
        Future<Boolean> submit = es.submit(() -> {
            try {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort));
                URLConnection connection = new URL("http://www.google.com").openConnection(proxy);
                connection.setConnectTimeout(1000);
                connection.connect();
                System.out.println("true");
                return true;
            } catch (Exception e) {
                System.out.println("false");
                return false;
            }
        });
        // block until the task you submitted to the thread pool completes execution and return a result(true or false). 
        return submit.get();
    }

The second way, in this way, the method will immediately return a Future, and again you need to call future#get which is block to get the result.

    public Future<Boolean> isProxyOnlineWithFuture(String proxyIp, int proxyPort) {
        return es.submit(() -> {
            try {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort));
                URLConnection connection = new URL("http://www.google.com").openConnection(proxy);
                connection.setConnectTimeout(1000);
                connection.connect();
                System.out.println("true");
                return true;
            } catch (Exception e) {
                System.out.println("false");
                return false;
            }
        });
    }

The return value of the isProxyOnline method is independent of the return value of the task you submitted to the thread pool. When you submit a task to the thread pool, you get a Future which reflects the result of your task execution.

Also you can consider using CompletableFuture or ListenableFuture: Listenablefuture vs Completablefuture.

zysaaa
  • 1,777
  • 2
  • 8
  • 19
  • I want to learn more about this topic. What area is that in Jave? – Swipi Nov 10 '21 at 12:54
  • I think you should start with multithreading in java. But for this question, you should learn the mechanics of thread pools. (```ExecutorService``` in java). Note that this has nothing to do with lambdas. @Swipi – zysaaa Nov 10 '21 at 13:09