Note that there are some explanatory texts on larger screens.

plurals
  1. POreturn value from a function running in separate thread
    primarykey
    data
    text
    <p>I have this code that allows to execute functions in a separate thread if "Asynch" annotation is present on them. Everything works fine, except for the day when I realized I also have to handle return value for some new functions that I've just added. I could use handlers and message-passing for this, but, due to already built project structure(which is huge and working fine), I can't change the existing functions to work with message passing. </p> <p>Here's the code:</p> <pre><code>/** * Defining the Asynch interface */ @Retention(RetentionPolicy.RUNTIME) public @interface Asynch {} /** * Implementation of the Asynch interface. Every method in our controllers * goes through this interceptor. If the Asynch annotation is present, * this implementation invokes a new Thread to execute the method. Simple! */ public class AsynchInterceptor implements MethodInterceptor { public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); Annotation[] declaredAnnotations = method.getDeclaredAnnotations(); if(declaredAnnotations != null &amp;&amp; declaredAnnotations.length &gt; 0) { for (Annotation annotation : declaredAnnotations) { if(annotation instanceof Asynch) { //start the requested task in a new thread and immediately //return back control to the caller new Thread(invocation.getMethod().getName()) { public void execute() { invocation.proceed(); } }.start(); return null; } } } return invocation.proceed(); } } </code></pre> <p>Now, how can i convert it so that if its something as:</p> <pre><code>@Asynch public MyClass getFeedback(int clientId){ } MyClass mResult = getFeedback(12345); </code></pre> <p>"mResult" gets updated with the returned value?</p> <p>Thanx in advance...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload