开发者

How do i by pass value from my thread to third party api thread and from third party thread change the value to my static variable in Java?

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

  pu开发者_如何学Cblic 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


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜