My problem is, i just want to return value from Multi threading out of 4 threads running concurrently in Multiple times. I need a return value for every times thread calls and run. Please justify me thank you, My code snippet looking like this, Thread1: i was calling java to native library function, function returns integer value.
 public void run() {
            double time = System.currentTimeMillis();
            println("Thread-1 starts " + time);
            ED emot = new ED();
            emot.edetect("str");
            try {
              Thread.sleep(1);
            } catch {
            }
            println("Value: " + emot.getvalue);
            i = emot.getvalue;
                }
                As far as I got your problem you have 4 threads which return some value after computation. Here are few ideas:
Callable<V> else Runnable.ExecutorService to submit your task (thread) and will get Future<V> or Future<?> depending on whether it is Callable or Runnable.future.get which blocks until result is received. If you want to receive result from any of the threads then in that case you can also use ExecutorCompletionService as well which maintains a queue of results in whatever order they are received.In multi threaded environment, we can return the values from thread by using Executer Service. This feature is available from JDK 5.
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html
Example :
public class MyThread implements Callable{
    @Override
    public String call(){
        Thread.sleep(2000);
        return "Hello";
    }
    public static void main(String args[]){
          ExecutorService executor = Executors.newFixedThreadPool(5);
          Callable<String> callable = new MyThread();
          String value = executor.submit(callable);
          System.out.println("The returned value is : "+value);
          executor.shutdown();
    }
}
This is the way you can return the values from thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With