[英]How do i reference a method from a different threads 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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.