我想要做的是修改一个线程内的全局ArrayList,并让这个修改的ArrayList在我的线程之外。我的意思是说,如果最后一个元素具有pollingCode,pollingType,轮询值= 0,那么这个元素有一个正确的大小,但这个ArrayList中的所有元素都是最后一个元素, 0,数组中的所有元素具有相同的值。从线程修改全局ArrayList
有谁知道为什么会发生这种情况?
public abstract class OverlayView extends RelativeLayout {
protected ArrayList<InputEvent> eventList = new ArrayList<InputEvent>();
public void StartEventMonitor() {
Thread b = new Thread(new Runnable() {
public void run() {
InputEvent ie= new InputEvent();
while (m_bMonitorOn) {
for (InputDevice idev:events.m_Devs) {
// Open more devices to see their messages
int pollingEvent = idev.getPollingEvent();
int pollingType=idev.getSuccessfulPollingType();
int pollingCode=idev.getSuccessfulPollingCode();
int pollingValue=idev.getSuccessfulPollingValue();
final String line =/* idev.getName()+
": " +*/ idev.getSuccessfulPollingType()+
" " + idev.getSuccessfulPollingCode() +
" " + idev.getSuccessfulPollingValue() +
"\n";
Log.d(LT, "Event:"+line);
ie.setCode(pollingCode);
ie.setType(pollingType);
ie.setValue(pollingValue);
eventList.add(ie);
}
}
}
}
});
b.start();
}
}
谢谢你。问题现在解决了;)我想这是我的错误,因为我总是试图避免新的对象创作,如果我不需要...通常当我添加元素到一个arrayList我通常使用相同的对象,只是改变值setter – 2013-03-18 15:32:06
@CarlosMoreradeLaChica无论什么价值 - 一般形式的对象创建并不昂贵,你不应该担心。有一些像SimpleDateFormat和其他报道是昂贵的。 Josh Bloch指出,20%的CPU时间花费在80%的代码上。 – 2013-03-18 15:34:11