1

I want to be able to wait to thread to return a value from some calculations done on another class. Let me post some code:

public class ClassA implements Runnable{

public boolean threadRunning ; 
public String value  ; 

public ClassA() {
    ...
    }

 public String getValueFromServer() {

  //call to a method in the server that does some calculation

  ClassB.sendPetitionToServer() ;
    t=new Thread (this) ;   
    threadRunning = true ; 
    t.start();
    return value
      }

    public void run()
          {
     while(threadRunning)   
        {
        checkValue ()
        }
      }


      public void checkValue () {
       String value = ClassB.getInstance().getValue ;
       if (value!=null) {
       threadRunning = false ; 
       }

     }

     }

The question is, how can I wait for the thread to return a value from the method from ClassB.SendPetitionFromToServer()? The method ClassB populates the value but has to make some calculations that take time (5-8 seconds). ClassB is not Runnable, is a simple class. Maybe should I make another class that stores the value and notify ClassA.

To summarise my question (Maybe is to confusing or even my approach just plain stupid).

I am developing a Java WebApp. I have a ClassA that implements Runnable interface. This class has a method that needs to retrieve some information from another ClassB that needs to perform some quite heavy calculations that take time (retrieving some info from MySQL, done some calculations with the info...). How can I stop the method from ClassA until I get the response from ClassB? Is there anyway to do it?

Thanks in advance

  • 1
    I don't understand why to use a thread to handle the heavy work if you will still wait until this thread finishes the work to continue... – Luiggi Mendoza Feb 11 '14 at 17:33
  • To learn more about what @LuiggiMendoza said, Google *Thread Safety*. Your thread is not safe if it introduces a race condition. – Rainbolt Feb 11 '14 at 17:38
  • @John I'm not talking about race conditions, I can think of this like: *Worker A can do this work W1 and W2, but W2 is heavy, so I will call Worker B to handle W2, but worker A must wait until worker B finishes its work to continue working*. In other words, you're not solving the performance problem in this case, but even using an additional thread =\. – Luiggi Mendoza Feb 11 '14 at 17:41
  • @John no, it is because OP has this odd design. It would be a race condition if both workers A and B modify and use the same resource, which doesn't seem to be the problem OP states. – Luiggi Mendoza Feb 11 '14 at 17:44
  • @John I understand what you say, but if you read the problem, the only thing to open a new thread here is because A needs to fulfill this variable with a value from B, which is retrieved only after W2 is finished. Apart from that, there's no need to use another thread unless some other work can be split apart and done in *parallel*. – Luiggi Mendoza Feb 11 '14 at 17:53
  • Many thanks for looking at my code. I know is odd and probably it could be done much better but I am a newbie in threads. Anyway 1000 thanks to everyone that has contributed. – Silikon Answer Feb 12 '14 at 09:57

1 Answers1

0

You can join() on the thread and then collect a value from a variable populated by the thread.

However I would instead investigate Executors and Futures, so you can spawn a thread via an Executor, get a Future and then call Future.get(). It's a nicer, higher-level mechanism for getting results from threads.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Many thanks for all the people that had answered. I will check all the proposals. I am totally new to threads so my code should be odd for sure but I will try to make it better looking to the proposals. Thanks Brian :-) – Silikon Answer Feb 12 '14 at 09:56