Java将集合作为参数传递,不添加到实际引用中
我将我的活动作为侦听器注册到我的服务中,并且在某些事件中,我传递服务内部保存的集合,这对于读取集合以使用我的列表适配器显示非常有用。但是,现在我想在回调中添加到集合中,但它不像我想象的那样工作!
//last param tells service to send the list
//first param sets listener that recieves the sent list
m_uploader.request(new RequestListener() {
@Override
public void getCompletedUploads(ArrayList<Job> completed) {
//dont care
}
@Override
public void getActiveUploads(ConcurrentLinkedQueue<Job> current) {
current.add(m_job); //doesnt add to queue in my service
ImportActivity.this.finish();
}
}, ServiceRequestArgs.create(RequestActiveUploadsCommand.class));
//impl of request()
public <T extends ICommand> void request(
IFileUploadListener listener, ServiceRequestArgs<T> args)
{
if (args.getClassToken().equals(RequestActiveUploadsCommand.class))
new RequestActiveUploadsCommand(listener, m_jobQueue).Execute();
if (args.getClassToken().equals(RequestCompleteUploadsCommand.class))
new RequestCompleteUploadsCommand(listener, m_completedList).Execute();
//calls getActiveUploads on listener
}
编辑 好的,所以我肯定只是传递引用的值。抛开对 Java 的厌恶不谈,我如何更改实际引用?
I have my activity register with my service as a listener, and on some events I pass the collection held internally in the service, which has worked great for reading the collection to display with my list adapter. However, now I want to add to the collection in my callback, but its not working like the reference I thought it was!
//last param tells service to send the list
//first param sets listener that recieves the sent list
m_uploader.request(new RequestListener() {
@Override
public void getCompletedUploads(ArrayList<Job> completed) {
//dont care
}
@Override
public void getActiveUploads(ConcurrentLinkedQueue<Job> current) {
current.add(m_job); //doesnt add to queue in my service
ImportActivity.this.finish();
}
}, ServiceRequestArgs.create(RequestActiveUploadsCommand.class));
//impl of request()
public <T extends ICommand> void request(
IFileUploadListener listener, ServiceRequestArgs<T> args)
{
if (args.getClassToken().equals(RequestActiveUploadsCommand.class))
new RequestActiveUploadsCommand(listener, m_jobQueue).Execute();
if (args.getClassToken().equals(RequestCompleteUploadsCommand.class))
new RequestCompleteUploadsCommand(listener, m_completedList).Execute();
//calls getActiveUploads on listener
}
edit Ok, so I am definately just passing the value of the reference. Java hating aside, how do I change the acctual reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对
current.add
的调用应该像您期望的那样将新的Job
添加到队列中。然而,Java 的并发集合有一些特殊的属性。一是 Concurrent 集合返回的
Iterator
是 弱一致:它可能反映也可能不反映自创建迭代器以来对集合所做的更改。这适用于您手动请求的迭代器和作为 for-each 的一部分自动创建的迭代器。
The call to
current.add
should be adding a newJob
to the queue like you expect.However, Java's Concurrent collections have some special properties. One is that an
Iterator
returned by a Concurrent collection is weakly consistent: it may or may not reflect changes made to the collection since the iterator was created.This applies to both
Iterator
s you manually request and ones that are automatically created as part of a for-each.