Home > Enterprise >  Getting false in Java Lambda boolean Method
Getting false in Java Lambda boolean Method

Time:11-10

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related