繁体   English   中英

Java使用单独的任务异步同步两个集合中的项目

[英]Java synchronize items in two collections asynchronously using separate task

我需要一些高效的代码来同步两个集合中的项目。 我的意思是,当一个项目添加到第一个集合时,它也应该添加到第二个集合中,但是将项目添加到第二个集合应该异步完成(例如,通过使用 Spring Boot 调度任务 API 循环调用的单独任务),以便两个集合包含相同的项目。

假设只有添加新项目的可能性

这些同步应该是有效的,以便该算法可以在生产环境中工作。 两个列表的同步最多需要几秒钟

能否请你帮忙 ?

提前致谢

我建议您在从第二个集合中读取时看同步的方向(理想情况是在第一个集合中记录时,但显然这是不可能的)。

Object get(Object key) {
    if (compareCollectionsBySize()) //it should be a simple, inexpensive operation
    {
        return duplicateCollection.get(key);
    } else {
        //we need synchronization
        //it all depends on the duration of the synchronization
        //and search costs for the original collection

        //in case fast synchronization
        synchronizeCollections();
        return duplicateCollection.get(key);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM