I would like to create an application which for example measures the execution time of a certain block of code. In this case it should have a structure like this:
public static long measureExecution(String code){
long start = System.nanoTime();
executeCode(code); // <----
long time = System.nanoTime() - start;
return time;
}
I'm curious about the method designated by the arrow, I need some sort of a placeholder. How should be this method implemented? Is it even possible to execute a custom Java code inside running Java application?
I was thinking that it can be done with some sort of overriding of another methods body, but I can't quite figure out how.
Thanks for your opinions!
You could pass a Runnable:
public static long measureExecution(Runnable code) {
long start = System.nanoTime();
code.run();
long time = System.nanoTime() - start;
return time;
}
At the place where you call the method, use an anonymous inner class to wrap the code you want to measure:
long time = measureExecution(new Runnable() {
#Override
public void run() {
System.out.println("Do something");
}
});
(If you were using Java 8, you could use a lambda expression instead of an anonymous inner class, which would make the code shorter and easier to read).
You can use OpenHFT/Java-Runtime-Compiler:
https://github.com/OpenHFT/Java-Runtime-Compiler
Also, you can use ToolProvider class (Compiler API), since java 1.6:
private Path compileJavaFile(Path javaFile, String className) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, javaFile.toFile().getAbsolutePath());
return javaFile.getParent().resolve(className);
}
You could use a Dynamic Proxy to wrap your methods invocation, here an example:
First you need to create InvocationHandler class:
public class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("About to invoke " + method + "\n with argument " + args);
Object rv = method.invoke(target, args);
System.out.println(" Call returned " + rv);// here you could print the time instead
return rv;
}
}
Then Create a factory to get you object and Wrap it with the previous created proxy.
public class MyFactory {
public static MyInterface getMyInterface() {
MyInterface mc = new MyClass();
InvocationHandler h = new MyInvocationHandler(mc);
MyInterface mi = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(),
new Class[] { MyInterface.class }, h);
return mi;
}
}
Hope that help you.
Related
How can I write code below in java version?
I have read similar questions, but they are confusing, they answered that java didn't have delegate feature like c# had, in other hand they answered with their delegate implementation in java, but nothing is similar with my condition.
I really hope it's clear on this question.
I have been getting stuck since a week
class Action
{
public delegate void ActionDelegate();
public static ActionDelegate OnAction;
public void DoAction()
{
Console.WriteLine("Action A");
if (!ReferenceEquals(OnAction, null))
OnAction();
}
}
class TaskA
{
public TaskA()
{
Action.OnAction += DoTaskA;
}
private void DoTaskA()
{
Console.WriteLine("Do Task A");
}
}
class TaskB
{
public TaskB()
{
Action.OnAction += DoTaskB;
}
private void DoTaskB()
{
Console.WriteLine("Do Task B");
}
}
class Program
{
static void Main(string[] args)
{
TaskA taskA = new TaskA();
TaskB task = new TaskB();
Action action = new Action();
action.DoAction();
}
}
Output:
Action A
Do Task A
Do Task B
Press any keys to continue...
something similar in java will be to use an interface
you can get the following results
Action A
Do Task A
Do Task B
with the codes below.
import java.util.ArrayList;
public class HelloWorld{
public static void main(String []args){
TaskA taskA = new TaskA();
TaskB task = new TaskB();
Action action = new Action();
action.doAction();
}
}
interface ActionDelegate {
void doAction();
}
class Action{
static public ArrayList<ActionDelegate> onAction = new ArrayList<>();
public void doAction(){
System.out.println("Action A");
for(ActionDelegate ad: onAction){
ad.doAction();
}
}
}
class TaskA implements ActionDelegate{
TaskA(){
Action.onAction.add(this);
}
public void doAction(){
System.out.println("Do Task A");
}
}
class TaskB implements ActionDelegate{
TaskB(){
Action.onAction.add(this);
}
public void doAction(){
System.out.println("Do Task B");
}
}
With the project safety-mirror on your classpath your get library support for delegates and events.
<dependency>
<groupId>com.github.hervian</groupId>
<artifactId>safety-mirror</artifactId>
<version>3.0.0</version>
</dependency>
Here's a snippet from the project's README:
Cheat sheet of features
Fun and friends: No more functional interfaces:
Fun.With0Params<String> myFunctionField = " hello world "::trim;
Fun.With2Params<Boolean, Object, Object> equals = Objects::equals;
public void foo(Fun.With1ParamAndVoid<String> printer) throws Exception {
printer.invoke("hello world);
}
foo(System.out::println); //This signature match the the Fun defined by method Foo. If it did not, the compiler would emit an error.
It is all type safe: you will get compile time errors if the Method Reference's signature does not match what is defined by the Fun subclass.
Method m1 = Fun.toMethod(String::isEmpty)
Method m2 = Fun.<String>toMethod(Class::forName)); // to get overloaded method you must specify parameters in generics
assertEquals("isEmpty", Fun.getName(String::isEmpty)); //use Fun's static getName method to get the method name. The Method objects returned from toMethod will not return the correct String.
Delegates in Java!
Delegate.With1Param<String, String> greetingsDelegate = new Delegate.With1Param<>();
greetingsDelegate.add(str -> "Hello " + str);
greetingsDelegate.add(str -> "Goodbye " + str);
DelegateInvocationResult<String> invocationResult = greetingsDelegate.invokeAndAggregateExceptions("Sir");
invocationResult.getFunctionInvocationResults().forEach(funInvRes -> System.out.println(funInvRes.getResult()));
//prints: "Hello sir" and "Goodbye Sir"
Events
//Create a private Delegate. Make sure it is private so only *you* can invoke it.
private static Delegate.With0Params<String> trimDelegate = new Delegate.With0Params<>();
//Create a public Event using the delegate you just created.
public static Event.With0Params<String> trimEvent= new Event.With0Params<>(trimDelegate);
Type safe method creation
Method m1 = Fun.toMethod(Thread::isAlive) // Get final method
Method m2 = Fun.toMethod(String::isEmpty); // Get method from final class
Method m3 = Fun.toMethod(BufferedReader::readLine); // Get method that throws checked exception
Method m4 = Fun.<String, Class[]>toMethod(getClass()::getDeclaredMethod); //to get vararg method you must specify parameters in generics
Method m5 = Fun.<String>toMethod(Class::forName); // to get overloaded method you must specify parameters in generics
Method m6 = Fun.toMethod(this::toString); //Works with inherited methods
Disclaimer: I am the author of the project.
I'm trying to create some instrumentation tool. I want to track each object allocation. The simplest idea that came to my mind was to retransform Object constructor as each object calls it (I know that arrays are initialized differently).
I tried use java agent mechanism, but it caused java.lang.instrument.UnmodifiableClassException. Obviously java agent cannot transform Object class at it is unmodifable.
Then I tried use JDI, where my debugged program looked like:
public class First {
public static int value = 1;
public static void main(String... args) throws InterruptedException {
while (true) {
print();
Thread.sleep(1000);
}
}
public static void print() {
System.out.println("Hello" + new Integer(value));
}
}
And debugger did only this:
VirtualMachine vm = new VMAcquirer().connect(8000);
List<ReferenceType> referenceTypes1 = vm.classesByName("java.lang.Object");
ReferenceType object = referenceTypes1.get(0);
if (vm.canRedefineClasses()) {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get("java.lang.Object");
CtConstructor constructor = ctClass.getConstructors()[0];
constructor.insertAfter("First.value += 1;");
HashMap<ReferenceType, byte[]> redefine = new HashMap<>();
redefine.put(object, ctClass.toBytecode());
ctClass.writeFile();
vm.redefineClasses(redefine);
}
vm.resume();
After that target program exits with message:
ERROR: JDWP Transport dt_socket failed to initialize, OUT_OF_MEMORY(110)
Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "main"
Do I do something wrong here? Is it possible to transform Object class that way?
I know about JVMTI but I wanted avoid C code, so is there any other way that does not require native code?
DISCLAIMER I'm aware of some similar questions already asked here (e.g. Hacking into java.lang.Object: calling custom external class crashes JVM or JDI, Java Byte code instrumentation and Java agents (JWDP, JVMTI)), but they doesn't explain to me everything.
---EDIT---
Transformed Object class looks like this:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package java.lang;
import jdk.internal.HotSpotIntrinsicCandidate;
public class Object {
private static native void registerNatives();
#HotSpotIntrinsicCandidate
public Object() {
Object var2 = null;
++First.value;
}
#HotSpotIntrinsicCandidate
public final native Class<?> getClass();
#HotSpotIntrinsicCandidate
public native int hashCode();
public boolean equals(Object obj) {
return this == obj;
}
#HotSpotIntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return this.getClass().getName() + "#" +
Integer.toHexString(this.hashCode());
}
#HotSpotIntrinsicCandidate
public final native void notify();
#HotSpotIntrinsicCandidate
public final native void notifyAll();
public final void wait() throws InterruptedException {
this.wait(0L);
}
public final native void wait(long var1) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0L) {
throw new IllegalArgumentException("timeout value is negative");
} else if (nanos >= 0 && nanos <= 999999) {
if (nanos > 0) {
++timeout;
}
this.wait(timeout);
} else {
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
}
/** #deprecated */
#Deprecated(
since = "9"
)
protected void finalize() throws Throwable {
}
static {
registerNatives();
}
}
I did also more tests and if I put something like int i = 1; int j = 2 + i; it works.
I also tried modify Integer constructor - this caused another exception:
Exception in thread "main" java.lang.NoClassDefFoundError: First
at java.base/java.lang.Integer.<init>(Integer.java:1075)
at First.print(First.java:13)
at First.main(First.java:7)
Class was successfully transformed, but at runtime there was a problem with linking to the class. I don't know if it is linked somehow. Maybe something similar happens with Object when some internal stuff tries to create new instance.
I was curious about Object var2 = null; line. Javaassist always puts ACONST_NULL bytecode, but it is not the cause of the problem.
---EDIT2---
I tried to transform another Object method. Transformation went succcessfully, but again error occured at runtime:
Exception in thread "main" java.lang.NoClassDefFoundError: First
at java.base/java.lang.Object.toString(Object.java:246)
at First.print(First.java:15)
at First.main(First.java:7)
For me it looks like the real problem is with NoClassDefFoundError. My assumption is it somehow related with classloader system in java (?). Could I somehow avoid such error? I don't know much about classloaders :/
I have a task to get "StackOverflowError" in java without using -Xss and recursion. I really don't have ideas... Only some nonsense like generating huge java class at runtime, compile it and invoke...
Java stores primitive types on the stack. Objects created in local scope are allocated on the heap, with the reference to them on the stack.
You can overflow the stack without recursion by allocating too many primitive types in method scope. With normal stack size settings, you would have to allocate an excessive number of variables to overflow.
Here is the implementation of Eric J. idea of generating excessive number of local variables using javassist library:
class SoeNonRecursive {
static final String generatedMethodName = "holderForVariablesMethod";
#SneakyThrows
Class<?> createClassWithLotsOfLocalVars(String generatedClassName, final int numberOfLocalVarsToGenerate) {
ClassPool pool = ClassPool.getDefault();
CtClass generatedClass = pool.makeClass(generatedClassName);
CtMethod generatedMethod = CtNewMethod.make(getMethodBody(numberOfLocalVarsToGenerate), generatedClass);
generatedClass.addMethod(generatedMethod);
return generatedClass.toClass();
}
private String getMethodBody(final int numberOfLocalVarsToGenerate) {
StringBuilder methodBody = new StringBuilder("public static long ")
.append(generatedMethodName).append("() {")
.append(System.lineSeparator());
StringBuilder antiDeadCodeEliminationString = new StringBuilder("long result = i0");
long i = 0;
while (i < numberOfLocalVarsToGenerate) {
methodBody.append(" long i").append(i)
.append(" = ").append(i).append(";")
.append(System.lineSeparator());
antiDeadCodeEliminationString.append("+").append("i").append(i);
i++;
}
antiDeadCodeEliminationString.append(";");
methodBody.append(" ").append(antiDeadCodeEliminationString)
.append(System.lineSeparator())
.append(" return result;")
.append(System.lineSeparator())
.append("}");
return methodBody.toString();
}
}
and tests:
class SoeNonRecursiveTest {
private final SoeNonRecursive soeNonRecursive = new SoeNonRecursive();
//Should be different for every case, or once generated class become
//"frozen" for javassist: http://www.javassist.org/tutorial/tutorial.html#read
private String generatedClassName;
#Test
void stackOverflowWithoutRecursion() {
generatedClassName = "Soe1";
final int numberOfLocalVarsToGenerate = 6000;
assertThrows(StackOverflowError.class, () -> soeNonRecursive
.createClassWithLotsOfLocalVars(generatedClassName, numberOfLocalVarsToGenerate));
}
#SneakyThrows
#Test
void methodGeneratedCorrectly() {
generatedClassName = "Soe2";
final int numberOfLocalVarsToGenerate = 6;
Class<?> generated = soeNonRecursive.createClassWithLotsOfLocalVars(generatedClassName, numberOfLocalVarsToGenerate);
//Arithmetic progression
long expected = Math.round((numberOfLocalVarsToGenerate - 1.0)/2 * numberOfLocalVarsToGenerate);
long actual = (long) generated.getDeclaredMethod(generatedMethodName).invoke(generated);
assertEquals(expected, actual);
}
}
EDIT:
The answer is incorrect, because it is one type of recursion. It is called indirect recursion https://en.wikipedia.org/wiki/Recursion_(computer_science)#Indirect_recursion.
I think the simplest way to do this without recursion is the following:
import java.util.LinkedList;
import java.util.List;
interface Handler {
void handle(Chain chain);
}
interface Chain {
void process();
}
class FirstHandler implements Handler {
#Override
public void handle(Chain chain) {
System.out.println("first handler");
chain.process();
}
}
class SecondHandler implements Handler {
#Override
public void handle(Chain chain) {
System.out.println("second handler");
chain.process();
}
}
class Runner implements Chain {
private List<Handler> handlers;
private int size = 5000; // change this parameter to avoid stackoverflowerror
private int n = 0;
public static void main(String[] args) {
Runner runner = new Runner();
runner.setHandlers();
runner.process();
}
private void setHandlers() {
handlers = new LinkedList<>();
int i = 0;
while (i < size) {
// there can be different implementations of handler interface
handlers.add(new FirstHandler());
handlers.add(new SecondHandler());
i += 2;
}
}
public void process() {
if (n < size) {
Handler handler = handlers.get(n++);
handler.handle(this);
}
}
}
At first glance this example looks a little crazy, but it's not as unrealistic as it seems.
The main idea of this approach is the chain of responsibility pattern. You can reproduce this exception in real life by implementing chain of responsibility pattern. For instance, you have some objects and every object after doing some logic call the next object in chain and pass the results of his job to the next one.
You can see this in java filter (javax.servlet.Filter).
I don't know detailed mechanism of working this class, but it calls the next filter in chain using doFilter method and after all filters/servlets processing request, it continue working in the same method below doFilter.
In other words it intercepts request/response before servlets and before sending response to a client.It is dangerous piece of code because all called methods are in the same stack at the same thread. Thus, it may initiate stackoverflow exception if the chain is too big or you call doFilter method on deep level that also provide the same situation. Perhaps, during debugging you might see chain of calls
in one thread and it potentially can be the cause of stackoverflowerror.
Also you can take chain of responsibility pattern example from links below and add collection of elements instead of several and you also will get stackoverflowerror.
Links with the pattern:
https://www.journaldev.com/1617/chain-of-responsibility-design-pattern-in-java
https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
I hope it was helpful for you.
Since the question is very interesting, I have tried to simplify the answer of hide :
public class Stackoverflow {
static class Handler {
void handle(Chain chain){
chain.process();
System.out.println("yeah");
}
}
static class Chain {
private List<Handler> handlers = new ArrayList<>();
private int n = 0;
private void setHandlers(int count) {
int i = 0;
while (i++ < count) {
handlers.add(new Handler());
}
}
public void process() {
if (n < handlers.size()) {
Handler handler = handlers.get(n++);
handler.handle(this);
}
}
}
public static void main(String[] args) {
Chain chain = new Chain();
chain.setHandlers(10000);
chain.process();
}
}
It's important to note that if stackoverflow occurs, the string "yeah" will never be output.
Of course we can do it :) . No recursion at all!
public static void main(String[] args) {
throw new StackOverflowError();
}
Looking at this answer below, not sure if this works for Java, but sounds like you can declare an array of pointers? Might be able to achieve Eric J's idea without requiring a generator.
Is it on the Stack or Heap?
int* x[LARGENUMBER]; // The addresses are held on the stack
int i; // On the stack
for(i = 0; i < LARGENUMBER; ++i)
x[i] = malloc(sizeof(int)*10); // Allocates memory on the heap
Im beginner JAVA developer. Here is a method:
private Method getSomething()
{
for (Method m : getClass().getDeclaredMethods())
{
return m;
}
return notFound;
}
private void notFound()
{
throw new Exception();
}
it doesnt matter what it does - if it finds something, then returns a Method - if not, the notFound() method itself should be returned. So the hot spot is at the return notFound; line: if I use return notFound(); then it returns its value, not the method itself. I want something like a reference/pointer. So getSomething() returns something what can be called, and if the returned method is used wrong, it should trigger that Exception - so its not an option to replace return notFound; with throw new Exception(); !
Or the 2nd option is to create a lambda method....
You need to call
this.getClass().getMethod("notFound")
to get the notFound method of the current/this object's class.
So just do this:
return this.getClass().getMethod("notFound");
More details here:
Class.getMethod
EDIT:
You can retrieve i.e. get and call private methods too via reflection.
Here is an example.
import java.lang.reflect.Method;
public class Test001 {
public static void main(String[] args) throws Exception {
Test002 obj = new Test002();
Method m = obj.getClass().getDeclaredMethod("testMethod", int.class);
m.setAccessible(true);
m.invoke(obj, 10);
m.invoke(obj, 20);
System.out.println(m.getName());
}
}
class Test002 {
private void testMethod(int x){
System.out.println("Hello there: " + x);
}
}
You need to use reflection to achieve this:
http://docs.oracle.com/javase/tutorial/reflect/
e.g. to get all methods of a given class:
Class aClass = ...//obtain class object
Method[] methods = aClass.getMethods();
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 have a GUI front end on this and it was crashing (hence why I've done the multithreading. My problem is I can no longer take the IP address as an argument in my ping code as its callable. I've searched all over for this and cant seem to find a way to get round this. Is there a way for a callable method to take arguments? If not is there any other way to accomplish what I'm trying to do?
sample of my code:
public class doPing implements Callable<String>{
public String call() throws Exception{
String pingOutput = null;
//gets IP address and places into new IP object
InetAddress IPAddress = InetAddress.getByName(IPtoPing);
//finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
//Results can vary depending on permissions so cmd method of doing this has also been added as backup
boolean reachable = IPAddress.isReachable(1400);
if (reachable){
pingOutput = IPtoPing + " is reachable.\n";
}else{
//runs ping command once on the IP address in CMD
Process ping = Runtime.getRuntime().exec("ping " + IPtoPing + " -n 1 -w 300");
//reads input from command line
BufferedReader in = new BufferedReader(new InputStreamReader(ping.getInputStream()));
String line;
int lineCount = 0;
while ((line = in.readLine()) != null) {
//increase line count to find part of command prompt output that we want
lineCount++;
//when line count is 3 print result
if (lineCount == 3){
pingOutput = "Ping to " + IPtoPing + ": " + line + "\n";
}
}
}
return pingOutput;
}
}
IPtoPing used to be the argument that was taken.
You can't pass it as the argument to call() because the method signature doesn't allow it.
However, you can pass the necessary information 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!!)
There are ways to eliminate some of the "boilerplate" coding in the above (see some of the other answers). In this case we are talking about 4 lines of code (in a ~40 line class), so I am not convinced that it is worth the effort. (But hey, it is your code.)
Alternatively, you could:
declare DoPing as an inner class (or a lambda) 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.)
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;
}
});
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
defining an abstract class that wraps/implements Callable and
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());
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.
Put some (final) fields in your doPing class, and a constructor that initializes them, then pass the values you want to use in call() to the constructor of doPing:
public class DoPing implements Callable<String> {
private final String ipToPing;
public DoPing(String ip) {
this.ipToPing = ip;
}
public String call() {
// use ipToPing
}
}
I know it is super-late to answer this, considering it is more than 8 years old but active 15 days(!) ago, I feel this will still help someone using Java 8 and above.
PS, it is simply a syntactic sugar of Victor Sorokin's answer possible through lambdas.
public static Callable<String> generateCallableWithArg(final String input) {
return () -> {
Thread.sleep(5000); // someExpensiveOperationHere
return "Return Value of " + input; //input can be used here
};
}
Also, we can write a static helper method that can convert a Function to Callable.
public class CallableGenerator {
public static <T,V> Callable<V> getCallableFromFunction(Function<T, V> function, T input) {
return () -> function.apply(input);
}
}
This can be used as
Callable<Integer> iAmCallable = CallableGenerator.getCallableFromFunction(i1 -> i1 * 2, 3);
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
}
}
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.