我有两个阶级作为阶级:如何从我在该类中创建的类访问类的变量?
public class A{
ArrayList<Runnable> classBList = new ArrayList<Runnable>();
int x = 0;
public A(){
//This code here is in a loop so it gets called a variable number of times
classBList.add(new B());
new Thread(classBList.get(classBList.size())).start();
}
}
public class B implements Runnable{
public B(){
}
public void run(){
//Does some things here. blah blah blah...
x++;
}
}
的问题是,我需要有B类的实例改变A类变量x,创建类B.然而类,我不知道我如何让B班知道它需要改变价值,或者如果可以的话。任何建议如何改变它将不胜感激。谢谢!
这个问题不清楚。你能重构你的问题吗? – 2013-02-18 09:09:55
你是否想用x计算“完成的任务”?你应该考虑同步...见http://www.vogella.com/articles/JavaConcurrency/article.html 3.2节 – Fildor 2013-02-18 09:16:22