1

I am writing a class that reads remote file data on a seperate thread.

This class inherits from thread and reads some remote file data in run method.I stored the data in a string.

Can i add other method that returns this data string?

i saw isalive return false value after run method returns.

Do i need to use event mechanism?

Please suggest some better workaround.

ramu
  • 1,019
  • 3
  • 15
  • 41

2 Answers2

1

Way#1

Use Callable instead, and get this string from a Future.

Check here for an example which returns a String

Like this:

public class GetDataCallable implements Callable<String> {  
    @Override
    public String call() throws Exception {
        return getDataFromRemote(); //get from file and returns
    }

}

and you use something like

    GetDataCallable <String> task = new GetDataCallable <String>();
    ExecutorService service = Executors.newFixedThreadPool(1);
    Future<String> future =service.submit(task);//run the task in another thread,
    ..//do any thing you need while the thread is runing 
    String data=future.get();//Block and  get the returning string from your Callable task

If you insist using Thread, you can try the below 2 ways

Way#2

public youThread extends Thread{
     private String data;//this is what you need to return

     @Override
     public void run(){
         this.data=//get from remote file.
     }

     public String getData(){
          return data;
     }
}

But you have to make sure the thread completes before you call getData(), for example , you can use thread.join() to do that.

thread.start()
..//some jobs else
thread.join();
String data=thread.getData();

Way#3

Use some static callback method at the end of your run()

Say

  public Class StoreDataCallback{
    public static void storeData(String data_from_thread){
          objectCan.setData(data_from_thread);//get access to your data variable and assign the value 
    }
}

public Class YourThread extends Thread{

     @Override
     public void run(){
       String data =getDataFromRemote();//do some thing and get the data.
       StoreDataCallback.storeData(data);
    }

}
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • Jaskey,thank you.by the time run method completes,isalive of the thread object is false.can i call getData method even if isalive is false? – ramu Nov 14 '14 at 09:39
  • @ramu, I does not tried that, check here http://stackoverflow.com/questions/17293304/when-is-a-java-thread-alive – JaskeyLam Nov 14 '14 at 09:45
0

I would use a Callable in your case, e.g.

Callable<String> callable = new Callable<String>() {

    @Override
    public String call() throws Exception {
        // do your work here and return the result
        return "Hello";
    }
};

Execute the callable either directly (in the same thread) or use an ExecutorService

ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> result = executorService.submit(callable);
System.out.println(result.get());

or just use FutureTask to execute a Callable

FutureTask<String> task = new FutureTask<String>(callable);
Thread taskRunner = new Thread(task); 
taskRunner.start();

String result = task.get();
René Link
  • 48,224
  • 13
  • 108
  • 140