java - 我如何引用来自不同线程java的方法
问题描述
我正在尝试从线程 b 引用线程 a,我本质上想在 B 类/线程中使用 getN() 方法,感谢任何帮助
//// class help {
///// main {
Thread a = new Thread(new A());
Thread b = new Thread(new B(a));
}
}
class A implements Runnable {
private static int tally;
public void run() {
}
public int getN() {
tally = 6;
return tally;
}
}
class B implements Runnable {
private A aref;
public B(A ref){
aref=ref;
}
public void run() {
aref.getN();
}
}
///////////////////////////////////////// ////////////////////////////////////
解决方案
为了构造 B 类的对象,您需要引用 A 类的对象,而不是 Thread 类的对象。所以这应该工作:
A objA = new A();
Thread a = new Thread(objA);
Thread b = new Thread(new B(objA));
推荐阅读
- python - 有 numpy 有限的 argsort 吗?
- json - 我需要解析字典并更新它的值
- python - 优化从字典数据集中检索数据
- python - 如何在 tkinter 中使用纵横比调整根窗口大小?
- url - 如何购买以one.***.com开头的域名
- javascript - 如何在不丢失单元格的情况下自动更新谷歌电子表格中的自定义公式?
- javascript - JSON 解析错误:window.onload() 中多个 HTTP 请求的意外标识符“未定义”
- python - Pyinstaller 抑制“DeprecationWarning”Python
- c# - 你如何从 LevelDB 读取/反序列化数据?
- google-sheets - 如何在 Google Data Studio 中为计数分配权重?