简体   繁体   English

Java和线程:非常奇怪的行为

[英]java and threads: very strange behaviour

        private synchronized Map<Team, StandingRow> calculateStanding() {
          System.out.println("Calculate standing for group " + getName());
          Map<Team, StandingRow> standing = new LinkedHashMap<Team, StandingRow>();

          for (Team team : teams) {
           standing.put(team, new StandingRow(team));
          }

          StandingRow homeTeamRow, awayTeamRow;
          for (Match match : matches.values()) {

           homeTeamRow = standing.get(match.getHomeTeam());
           awayTeamRow = standing.get(match.getAwayTeam());

           System.out.println("Contains key for " + match.getHomeTeam() + ": " + standing.containsKey(match.getHomeTeam()));
           System.out.println("Contains key for " + match.getAwayTeam() + ": " + standing.containsKey(match.getAwayTeam()));
                }
        }

This is my code. 这是我的代码。 matches contains 6 elements, but the problem is that after two matches no keys are anymore found in the standing map. 比赛包含6个元素,但是问题是两次比赛之后,站立地图中再也找不到钥匙。

The output is for example 输出例如

Contains key for Zuid-Afrika: true
Contains key for Mexico: true
Contains key for Uruguay: true
Contains key for Frankrijk: true
Contains key for Zuid-Afrika: false
Contains key for Uruguay: false
Contains key for Frankrijk: false
Contains key for Mexico: false
Contains key for Mexico: false
Contains key for Uruguay: false
Contains key for Frankrijk: false
Contains key for Zuid-Afrika: false

This is in a threaded environment, but the method is synchronized so I thought that this would not give a problem? 这是在线程环境中,但是方法是同步的,所以我认为这样不会带来问题? I have also a simple unit test for this method and that works well. 我对此方法也有一个简单的单元测试,效果很好。

This is almost surely not a threading issue. 这几乎肯定不是线程问题。 I'm all but certain that the problem is in your Team class. 我几乎可以肯定,问题出在您的团队班上。 It's probably not implementing hashCode() / equals() the right way. 它可能没有以正确的方式实现hashCode() / equals() Review the javadoc for these two methods and implement them accordingly. 查看这两个方法的javadoc并相应地实现它们。

Can you give the details of the Team and the Match classes? 您能否提供TeamMatch类别的详细信息? Are they implementing equals() and hashCode() ? 他们在实现equals()hashCode()吗?

Since Team and Match are your own classes, you have to tell java how it should match (decide whether they are equal ) 由于Team和Match是您自己的类,因此您必须告诉java它应该如何匹配 (确定它们是否相等 )。

You do that using equals() and hashCode() . 您可以使用equals()hashCode() See also this article . 另请参阅本文

Assuming you have implemented equals() and hashCode() correctly , your method looks fine to me. 假设您正确实现了equals()hashCode() ,那么您的方法对我来说就很好。

The big question is: Where does teams and matches come from and how are they filled? 最大的问题是: teamsmatches从何而来?如何填补?

If teams and matches are changed by different threads without synchronization on the same monitor, you could get race conditions there. 如果teamsmatches由不同的线程更改而没有在同一监视器上同步,那么您可以在那里获得比赛条件。

Try to sync the methods that are changing matches and teams also. 尝试同步更改matchesteams的方法。

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

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