I appeared for an Interview today . the interviewer asked me to write a thread in java 1.5
.Either you can extend java.lang.thread class or implement java.lang.Runnable interface.
Is it possible that implemented run() method have a return type other than void.
The method signature in Runnable interface is
public void run();
My answer was “you can not return an int or string from a run() method because of above signature definition.”
the interviewer said in java 1.5 you can return int or string from run method.
can you all please tell me how it can be done ?
I tried below it gave compilation error :
public class SampleInterview implements Runnable {
@Override
public int run() {
System.out.println("Start : inside run of SampleInterview");
System.out.println("End : inside run of SampleInterview");
}
}
I have searched google also could not find any such way. Is it possible or not ?
It’s not possible; either you misunderstood the interviewer, or they were wrong. (You might just “write the return value into some shared variable,” but that doesn’t seem like it meets the requirements.)
The
Callableinterface, though, lets you return a value.