我如何获取/设置“目标”值?
- 这样我也可以从其他类或线程中使用它?我尝试过这个,但它总是给出空或什么都没有,而不是向我显示“5”个目标。
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");
}
});
}
}
注意:我现在正在单独尝试,触发一个事件>新线程作为抽象接口(interface)>将该接口(interface)实现为线程,并从该线程将变量分配给主类静态变量。然后监听虚拟线程。所以总共我可能有: Main > Thread1 > ThirdpartyThred > 抽象接口(interface) > Thread2 > Main put/get
最佳答案
定义一个带有完整方法的回调接口(interface),该方法接受任何对象作为参数
在Thread(或Runnable)类中传递对启动线程的类的引用。当线程上的操作完成后,调用回调方法
Java executors: how to be notified, without blocking, when a task completes?
关于java - 我如何将值从我的线程传递到第三方 api 线程,并从第三方线程将值更改为 Java 中的静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7385466/