1

How can i get/set the "goal" value?

  • So that i can also use it from other class or threads? I tried this but its always giving null or nothing instead of showing me "5" goals.

Main.java:

public class Main
{
  public static String goal = null;
  public static void main(String[] args)
  {
    System.out.println(goal); // shows: null
    MyFunction1();
    System.out.println(goal); // How many goals happend till now?
  }

  public static void MyFunction1()
  {
    new Thread(new Runnable()
    {
      public void run() 
      {
        CallMe();
        System.out.println("show me: " + goal); // shows nothing.
      }
    }).start();
  }

  public static void CallMe()
  {
      ThirdpartySoftware.Bla().connect(new Bla.STATE()
      { 
        public void stateChanged() 
        {         
           System.out.println("Am i running? yes");    
           goal = "5";
           System.out.println("Did i assigned new value to goal? yes");
        } 
      });     
  }

}

Note: I am now separately trying, to fire a event > new thread as abstract interface > implement that interface as a thread and from that thread assign the variable to main class static variable. And then listen on a virtual threads. So in total i may have: Main > Thread1 > ThirdpartyThred > Abstract interface > Thread2 > Main put/get

  • The problem is that you don't want to see its current value, you want to wait until it is set. This simplest solution is not to do this and have the code you want to run after the value is set, to be after the code where value is set and nowhere else. This what the event driven call back is for. – Peter Lawrey Sep 12 '11 at 09:07
  • Define a Callback interface with a complete method as below http://stackoverflow.com/questions/826212/java-executors-how-to-be-notified-without-blocking-when-a-task-completes – sfk Sep 12 '11 at 09:38

2 Answers2

0

You connect to the third party and add a listener. then you check your output.

But you only added a listener. Later on when the state changes then your goal should be set but nothing says the connect method itself will change it.

Peter
  • 5,728
  • 20
  • 23
  • when i run it i see the connect method is executed successfully. And my goal inside connect method shows 5. But from other class/thread i cant see goal last result, which is always null or nothing except connect method. –  Sep 12 '11 at 09:21
0

Define a Callback interface with a complete method that takes any object as an argument

In the Thread (or Runnable) class pass in the reference to the class that started the thread. When the operation on the thread completes, call the callback method

Java executors: how to be notified, without blocking, when a task completes?

Community
  • 1
  • 1
sfk
  • 625
  • 1
  • 9
  • 17
  • that does not help because my listener never ends (its a webcam third party licensed SDK). I need to jump out from that so that i can have global status, for accessing from other threads/class. Your advise is still not helping. –  Sep 12 '11 at 12:24