我如何从不同的线程引用方法 java
How do i reference a method from a different threads java
我正在尝试从线程 b 引用线程 a,我基本上想在 B class/thread 中使用 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();
}
}
/////////////////////////////////////////// /////////////////////
////////////////////////
为了构造 class B 的对象,您需要引用 class A 的对象,而不是 class Thread 的对象。所以这应该有效:
A objA = new A();
Thread a = new Thread(objA);
Thread b = new Thread(new B(objA));
我正在尝试从线程 b 引用线程 a,我基本上想在 B class/thread 中使用 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();
}
}
/////////////////////////////////////////// ///////////////////// ////////////////////////
为了构造 class B 的对象,您需要引用 class A 的对象,而不是 class Thread 的对象。所以这应该有效:
A objA = new A();
Thread a = new Thread(objA);
Thread b = new Thread(new B(objA));