Java 迭代未来对象的映射并调用其get函数

Java 迭代未来对象的映射并调用其get函数,java,hashmap,iteration,future,concurrent.futures,Java,Hashmap,Iteration,Future,Concurrent.futures,我在交互一个HashMap时遇到了困难: for(条目:map.entrySet()){ debug(“线程:+entry.getKey()+”/” +entry.getValue()); Future fut=entry.getValue(); 试一试{ dpsimporterservicepimpl imp=fut.get(); debug(“等待:”+imp.toString()); }捕捉(中断异常e){ e、 printStackTrace(); }捕获(执行例外){ e、 print

我在交互一个
HashMap
时遇到了困难:

for(条目:map.entrySet()){
debug(“线程:+entry.getKey()+”/”
+entry.getValue());
Future fut=entry.getValue();
试一试{
dpsimporterservicepimpl imp=fut.get();
debug(“等待:”+imp.toString());
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
我的映射包含两个条目:
{1435748953925=java.util.concurrent。FutureTask@705f23aa,1435748971395=java.util.concurrent。FutureTask@761ea788}

以下是我的结果:

Thread: 1435748953925/java.util.concurrent.FutureTask@705f23aa
Waiting over for: Thread busy with 1435748971395.
Thread: 1435748971395/java.util.concurrent.FutureTask@761ea788
Waiting over for: Thread busy with 1435748971395.
正如您所看到的,映射得到了正确的迭代,因为以
Thread…
开头的日志语句是映射中的语句。 但是,
get()
方法不能正常工作,因为它显然是从同一个对象调用的(两次都是从第二个条目中使用键
1435748971395

这让我很困惑,因为地图上肯定没有相同的对象


你能告诉我这里可能出了什么问题吗?谢谢大家!

好吧,以下代码段按预期工作:

Map<String, Future<String>> map = new HashMap<>();

ExecutorService service = new ScheduledThreadPoolExecutor(1);

Future<String> future1 = service.submit(() -> {
    Thread.sleep(1000);
    return "1";
});
Future<String> future2 = service.submit(() -> {
    Thread.sleep(1000);
    return "2";
});

map.put("1", future1);
map.put("2", future2);

for (Map.Entry<String, Future<String>> entry : map.entrySet()) {
    System.out.println("Thread: " + entry.getKey() + "/" + entry.getValue());
    Future<String> fut = entry.getValue();
    try {
        String imp = fut.get();
        System.out.println("Waiting over for: " + imp);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}

service.shutdown();
Map Map=newhashmap();
ExecutorService=新的ScheduledThreadPoolExecutor(1);
Future future1=服务。提交(()->{
睡眠(1000);
返回“1”;
});
Future future2=服务。提交(()->{
睡眠(1000);
返回“2”;
});
地图放置(“1”,未来1);
地图放置(“2”,未来2);
对于(Map.Entry:Map.entrySet()){
System.out.println(“线程:”+entry.getKey()+“/”+entry.getValue());
Future fut=entry.getValue();
试一试{
字符串imp=fut.get();
System.out.println(“等待:+imp”);
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}
}
service.shutdown();

在我看来,问题可能出在DPSImporterServiceImpl中,或者是在地图中获得这些未来的方式上。

我将发布您的
DPSImporterServiceImpl#toString
实现。它可能代表一个类范围的状态而不是实例。在我看来,
DPSImporterServiceImpl
类的
toString()
实现没有正确实现?你能发布DPSImporterServiceImpl的代码吗
DPSImporterServiceImpl
的代码相当复杂。
toString()
方法不是问题,当我对它进行注释时,我得到以下日志:Thread:1435748953925/java.util.concurrent。FutureTask@7d137980正在等待:mypackage.service.impl。DPSImporterServiceImpl@2c9371e线程:1435748971395/java.util.concurrent。FutureTask@295b7fd2正在等待:mypackage.service.impl。DPSImporterServiceImpl@2c9371e所以它仍然是同一个物体。你说地图上肯定没有相同的物体。。。好的,映射包含未来,而不是DPSImporterServiceImpl。也许DPSImporterServiceImpl是独一无二的?像单身汉?它们是如何产生的?