omurbek
omurbek

Reputation: 772

Java passing value between two classes

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
    }
}

Upvotes: 1

Views: 77

Answers (1)

Andreas Brunnet
Andreas Brunnet

Reputation: 732

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();
    }

Upvotes: 3

Related Questions