Is there a way to take an argument in a callable method?

后端 未结 7 1020
Happy的楠姐
Happy的楠姐 2020-12-23 16:41

I have created a piece of code which takes an IP address (from main method in another class) and then loops through a range of IP addresses pinging each one as it goes. I ha

相关标签:
7条回答
  • 2020-12-23 16:56

    When you create the doPing-class (should be captial letter in class name), send in the ip-address in the constructor. Use this ip-address in the call-method.

    0 讨论(0)
  • 2020-12-23 17:01

    It is not always possible to make reference to (effectively) final variable to use its value as "argument", but you can make comfy general solution by yourself. First define this functional interface:

    @FunctionalInteface
    interface CallableFunction<T, R> {
    
        public abstract R call(T arg) throws Exception;
    
        public static <T, R> Callable<R> callable(CallableFunction<T, R> cf, T arg) {
            return () -> cf.call(arg);
        }
    }
    

    This functional interface provides static method callable that creates a Callable instance, which simply calls call(T) with provided argument (of type T). Then you need you DoPing class to implement CallableFunction like this:

    public class DoPing implements CallableFunction<String, String> {
    
        @Override
        public String call(final String ipToPing) throws Exception {
            final var ipAddress = InetAddress.getByName(ipToPing);
            final var reachable = ipAddress.isReachable(1400);
            String pingOutput = null;
            if (reachable) {
                pingOutput = ipToPing + " is reachable.\n";
            }
            else {
                final var ping = Runtime.getRuntime().exec("ping " + ipToPing + " -n 1 -w 300");
                try (var in = new BufferedReader(new InputStreamReader(ping.getInputStream()))) {
                    String line;
                    for (int lineCount = 1; (line = in.readLine()) != null; ++lineCount) {
                        if (lineCount == 3) {
                            pingOutput = "Ping to " + ipToPing + ": " + line + "\n";
                            break;
                        }
                    }
                }
            }
            return pingOutput;
        }
    

    Here we changed call signature to accept String argument and also now it implements CallableFunction and not Callable as before. Other changes are minor, but it's worth mentioning, that we prevented resource leak with use of try-with-resource on BufferedReader and also break has been added to input collecting loop (change from while to for) to terminate as quickly, as possible.

    Now you can use the code e.g. like this:

        final var ping = CallableFunction.callable(new DoPing(), "127.0.0.1");
        final var task = new FutureTask<>(ping);
        new Thread(task).start();
        System.out.println(task.get(20, TimeUnit.SECONDS));
    

    You can also reuse CallableFunction in other cases, when you needed it.

    0 讨论(0)
  • 2020-12-23 17:04

    You can't pass it as the argument to call() because the method signature doesn't allow it.

    However, you can pass it as a constructor argument; e.g.

    public class DoPing implements Callable<String>{
        private final String ipToPing;
    
        public DoPing(String ipToPing) {
            this.ipToPing = ipToPing;
        }
    
        public String call() throws SomeException {
            InetAddress ipAddress = InetAddress.getByName(ipToPing);
            ....
        }
    }
    

    (I've corrected a couple of egregious code style violations!!)

    Alternatively, you could:

    • declare DoPing as an inner class and have it refer to a final ipToPing in the enclosing scope, or

    • add a setIpToPing(String ipToPing) method.

    (The last allows a DoPing object to be reused, but the downside is that you will need to synchronize to access it thread-safely.)

    0 讨论(0)
  • 2020-12-23 17:04

    You have to defien a property such as ipAddress and its accessor method. and passing its value in constructor or by setter method. In doPing class use ipAddress property.

    class DoPing/* In java all classes start with capital letter */implements Callable<String>
    {
        private String  ipAddress;
    
        public String getIpAddress()
        {
            return ipAddress;
        }
    
        public void setIpAddress(String ipAddress)
        {
            this.ipAddress = ipAddress;
        }
    
        /*
         * Counstructor 
         */
        public DoPing(String ipAddress )
        {
            this.ipAddress = ipAddress;
        }
    
        @Override
        public String call() throws Exception
        {
            // your logic
        }
    }
    
    0 讨论(0)
  • 2020-12-23 17:06

    You can't pass arguments to call() because the method signature doesn't allow it but here is at least one way to work around that by

    1. defining an abstract class that wraps/implements Callable and
    2. implementing a setter to "inject" a result into call()

    Define an abstract class:

    import java.util.concurrent.Callable;
    
    public abstract class Callback<T> implements Callable<Void> {
        T result;
    
        void setResult (T result) {
            this.result = result;
        }
    
        public abstract Void call ();
    }
    

    Define the method that should fire the callback:

    public void iWillFireTheCallback (Callback callback) {
        // You could also specify the signature like so:
        // Callback<Type of result> callback
    
        // make some information ("the result")
        // available to the callback function:
        callback.setResult("Some result");
    
        // fire the callback:
        callback.call();
    }
    

    In the place where you want to call iWillFireTheCallback:

    Define the callback function (even possible inside methods):

    class MyCallback extends Callback {
        @Override
        public Void call () {
            // this is the actual callback function
    
            // the result variable is available right away:
            Log.d("Callback", "The result is: " + result);
    
            return null;
        }
    }
    

    And then call iWillFireTheCallback while passing in the callback:

    iWillFireTheCallback(new MyCallback());
    
    0 讨论(0)
  • 2020-12-23 17:09

    Adding to Jarle's answer -- in case you create Callable as instance of anonymous class, you can use final field outside of anonymous class for passing data into the instance:

        final int arg = 64;
        executor.submit(new Callable<Integer>() {
            public Integer call() throws Exception {
                return arg * 2;
            }
        });
    
    0 讨论(0)
提交回复
热议问题