Java将集合作为参数传递,不添加到实际引用中

发布于 2024-10-22 08:45:55 字数 1210 浏览 0 评论 0原文

我将我的活动作为侦听器注册到我的服务中,并且在某些事件中,我传递服务内部保存的集合,这对于读取集合以使用我的列表适配器显示非常有用。但是,现在我想在回调中添加到集合中,但它不像我想象的那样工作!

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生池水覆流年 2024-10-29 08:45:55

current.add 的调用应该像您期望的那样将新的 Job 添加到队列中。

然而,Java 的并发集合有一些特殊的属性。一是 Concurrent 集合返回的 Iterator弱一致:它可能反映也可能不反映自创建迭代器以来对集合所做的更改。

这适用于您手动请求的迭代器和作为 for-each 的一部分自动创建的迭代器。

The call to current.add should be adding a new Job 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 Iterators you manually request and ones that are automatically created as part of a for-each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文