-2

Is there a way for me to pass the value of "r" once this thread completes? I believe I did this 9 or 10 months ago on another project but for the l ife of me I cannot remember the project of find the code. I've spent about 5 hours digging through code and googling but have fell short on a solution and since I do not develop in Java\Android often, this has completely slipped my mind.

Below is my code and what I think I remember from the last time I did this. I kind of remember a ".call()" or ".get()" but after trying to find something in the android docs. I'm not so sure. I could only find "callable".

public void updateUserProfile(){

if(checkFields(){
//Do Something
}else{
//Do Something
}

}


 public boolean checkFields(){

Boolean r = false;



     Runnable run = new Runnable(){
                    //Boolean r;
                @Override
                public void run() {

                    //Or boolean r;

                    //get value of r.
                    r = php.UpdateProfile.updateProfile(user_id, user_email);
                }
            };

            new Thread(run).start();

            return r; //run.get(r)?
}
TyJacobs
  • 1
  • 2
  • 2
  • 5
  • Your response seems to be counter productive. If it is simple then help with a solution and not a smart @$$ comment. – TyJacobs May 17 '15 at 17:05
  • maybe a delegate could help you solving your problem – Blackbelt May 17 '15 at 17:07
  • can you post more code and show us where you need the r variable? – Alexanus May 17 '15 at 17:07
  • @TyJacobs try my answer. And please be respectful towards fellow members. – AAnkit May 17 '15 at 17:11
  • Updated code to show how I am using it. I could use AsyncTask. – TyJacobs May 17 '15 at 17:18
  • You earn respect AAnkit. A smart @$$ comment does not at all merit respect from me. I do not care about "reputation points". I too am a registered member and from what I understand of Stack is his comment should be removed or down voted because he is not providing anything in regards to the question. No one will ever look at m0skit0's comment and be like "oh, that's the solution". – TyJacobs May 17 '15 at 17:22
  • Yet another "smart ass" suggestion: you can also spend 5h learning how to use Google. First result on search: http://stackoverflow.com/questions/9148899/returning-value-from-thread – m0skit0 May 17 '15 at 18:49
  • See this is where it is should not be allowed to post or comment or really breathe for that matter. do you honestly think that every person in the entire world what have ordered a search exactly as you have – TyJacobs May 17 '15 at 18:54

5 Answers5

5

What you are doing is creating an instance of an anonymous inner class implementing Runnable. As such you can access the fields of the containing class. Store the result of your code in one of those.

Of course be careful to synchronise access to this result variable between your main thread and the one you create here.

public class TestThreads {
    static int r = 0;
    public static void main(String[] args) {
        Runnable run = new Runnable() {
            public void run() {
                r = 20;
            }
        };
        Thread t = new Thread(run);
        t.start();
        try {
            t.join();
        }
        catch(InterruptedException e) {
            return;
        }
        System.out.println("Result: " + r);
    }
}
krcools
  • 897
  • 5
  • 12
0

You cannot return values from method which has return type Void.

Use Handler class to send messages, and from thread's run method, call handler.sendMessage() and handle this message on onHandleMessage of Handler

Handler Document link

Handler example

AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

Try with Future, Callable, ExecuterService ... This might help http://www.journaldev.com/1090/java-callable-future-example

Mithun
  • 2,075
  • 3
  • 19
  • 26
0

Runnable does not return. You will need to use Future that implements Callable or use FutureTask.

ExecutorService pool = Executors.newFixedThreadPool(poolSize);
FutureTask<String> future =
   new FutureTask(new Callable() {
   public String call() {
      String res = ...;
      return res;
});
pool.execute(future);

String res = future.get(); 
// use the line below if you want to set a timeout for the task
String res = future.get(timeoutmillis, TimeUnit.MILLISECONDS);
MPeti1
  • 33
  • 6
inmyth
  • 8,880
  • 4
  • 47
  • 52
-1

I ended up using AsyncTask. Was hoping to keep it super simple because of support requirements but AsyncTask did what I needed it to.

private class updateUserProfile extends AsyncTask<Boolean, Boolean, Boolean>{


        @Override
        protected Boolean doInBackground(Boolean... params) {

            return php.UpdateProfile.updateProfile(etEditAddress.getText().toString(),
                    etEditCity.getText().toString(),
                    spnStates.getSelectedItem().toString(), etEditZip.getText().toString(), etEditPhone.getText().toString(), etEditAddress.getText().toString());;
        }

        public void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            Toast.makeText(MyAccount.this, result.toString(), Toast.LENGTH_LONG).show();
        }

    }
TyJacobs
  • 1
  • 2
  • 2
  • 5