Maison >Java >javaDidacticiel >Pourquoi la synchronisation sur les objets chaîne en Java ne fonctionne-t-elle pas toujours ?

Pourquoi la synchronisation sur les objets chaîne en Java ne fonctionne-t-elle pas toujours ?

Patricia Arquette
Patricia Arquetteoriginal
2024-11-14 13:32:021054parcourir

Why Doesn't Synchronizing on String Objects in Java Always Work?

How to Synchronize on String Objects in Java

When implementing concurrency in Java, it's important to understand the consequences of synchronizing on different types of objects. This article explores a common issue that arises when synchronizing on String objects and provides solutions to ensure effective synchronization.

The Problem

The provided scenario involves a web service that uses a cache to store responses for certain endpoints. The goal was to ensure that only one thread would call the web service if the cached object was expired. To achieve this, the code synchronized on the cache key, which was a String object. However, the synchronization didn't seem to work as expected, with multiple threads entering the synchronization block simultaneously.

The Solution: Interning Strings

The issue in the example code is that instances of String objects with the same value are not necessarily the same object. Therefore, when creating the cache key with String concatenation ("Data-" + email), a new String object is created for each key. This means that the code was actually synchronizing on different String objects, even though they had the same value.

To solve this, the intern() method can be used on the String object. The intern() method returns the canonical representation of the string, which is a single instance that is used for all occurrences of that string in the Java Virtual Machine (JVM). By using the canonical representation, all threads will be synchronizing on the same String object, ensuring proper synchronization.

Example with Interning

The updated code with string internment:

private SomeData[] getSomeDataByEmail(WebServiceInterface service, String email) {

  final String firstkey = "Data-" + email;
  final String key = firstkey.intern();
  
  synchronized(key) {
    // Rest of the original code
  }
}

Conclusion

By using intern() to ensure that all String objects are represented by the canonical representation, the synchronization mechanism in the provided code can be made effective. It's important to remember that synchronizing on different instances of objects with the same value can lead to synchronization failures, and using intern() can help prevent this issue.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn