1

I am trying to create a new thread process, after thread process ends i want to get a result from that class.how can i do it?
For example this two classes. Lets say abThread class returns String array. How should I get those String values.

Class A{

    public static void main(String[] args){
        abThread bb=new abThread();
        bb.start();
        // when bb.run() returns data catch it
    }
}

Class abThread extends Thread{
    public void run(){
       **// do smth here**
        // then return result to the main Class
    }
}
TMob
  • 1,278
  • 1
  • 13
  • 33
omurbek
  • 742
  • 1
  • 7
  • 23

1 Answers1

3

What you are looking for is a Callable like so:

 public class MyCallable implements Callable<String[]>
    {

        @Override
        public String [] call() throws Exception
        {
            //Do your work
            return new String[42]; //Return your data
        }

    }
    public static void main(String [] args) throws InterruptedException, ExecutionException
    {
        ExecutorService pool = Executors.newFixedThreadPool(1);
        Future<String[]> future = pool.submit(new MyCallable());

        String[] myResultArray = future.get();
    }
Andreas Brunnet
  • 722
  • 6
  • 19