DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Software Design and Architecture Topics

article thumbnail
ConcurrentHashMap isn't always enough
When Java developers come to a task of writing a a new class which should have a Map datastructure field, accessed simultaneously by several threads, they usually try to solve the synchronization issues invloved in such a scenario by simply making the map an instance of ConcurrentHashMap . public class Foo { private Map theMap = new ConcurrentHashMap<>(); // the rest of the class goes here... } In many cases it works fine just because the contract of ConcurrentHashMap takes care of the potential synchronization issues related to reading/writing to the map. But there are cases where it's not enough, and a developer gets race conditions which are hard to predict, and even harder to find/debug and fix. Let's have a look, at the next example: public class Foo { private Map theMap = new ConcurrentHashMap<>(); public Object getOrCreate(String key) { Object value = theMap.get(key); if (value == null) { value = new Object(); theMap.put(key, value); } return value; } } Here we have a "simple" getter ( getOrCreate(String key) ), which gets a key and returns the value assosiated with the given key in theMap . If there is no mapping for the key, the method creates a new value, inserts it into theMap and returns it. So far so good. But what happens when 2 (or more) threads call the getter with the same key when there is no mapping for the key in theMap? In such a case we might receive a race condition: Suppose thread t1 enters the function and comes to line 7. Its value is null . At this point thread t2 enters the function and also comes to line 7. Its value is also obviously null . Therefore from this point the two threads will enter the if statement and execute lines 8 and 9, thus creating two different new Objects. Upon returning from the getter each thread will get a different Object instance, violating programmer's wrong assumption that by using ConcurrentHashMap "everything is synchronized" and therefore two different threads should get the same value for the same key. To solve this issue we can synchronize the entire method, thus making it atomic: public class Foo { private Map theMap = new ConcurrentHashMap<>(); public synchronized Object getOrCreate(String key) { Object value = theMap.get(key); if (value == null) { value = new Object(); theMap.put(key, value); } return value; } } But this is a bit ugly, and uses Foo instace's monitor, which may affect performance if there are other methods in this class which are synchronized. Also a common rule of thumb is to try to eliminate using synchronized methods as much as possible. A much better approach should be using Java 8 Map's computeIfAbsent(K key, Function mappingFunction), which, in ConcurrentHashMap's implementation runs atomically: public class Foo { private Map theMap = new ConcurrentHashMap<>(); public Object getOrCreate(String key) { return theMap.computeIfAbsent(key, k -> new Object()); } } The atomicity of computeIfAbsent(..) assures that only one new Object will be created and put into theMap, and it'll be the exact same instance of Object that will be returned to all threads calling the getOrCreate function. Here, not only the code is correct, it's also cleaner and much shorter. The point of this example was to introduce a common pitfall of blindly relying on ConcurrentHashMap as a majical synchronzed datastructure which is threadsafe and therefore should solve all our concurrency issues regarding multiple threads working on a shared Map. ConcurrentHashMap is, indeed, threadsafe. But it only means that all read/write operations on such map are internally synchronized. And sometimes it's just not enough for our concurrent environment needs, and we have to use some special treatment which will guarantee atomic execution. A good practice will be to use one of the atomic methods implemented by ConcurrentHashMap, i.e: computeIfAbsent(..), putIfAbsent(..), etc.
December 8, 2016
by Dima Leah
· 48,823 Views · 12 Likes
article thumbnail
Message-Based Security for SOAP in webMethods: Part I
Learn how to place policies under Integration Server, attach a policy to web service descriptor, and pass message-based authentication credentials with web service.
December 8, 2016
by Prasad Pokala
· 8,159 Views · 7 Likes
article thumbnail
Real-Time Data Batching With Apache Camel
If you want your message flow to scale, it takes some work. This proposal, using Apache Camel, let's you handle high volume requests with a variety of databases.
December 8, 2016
by Ben O'Day
· 21,912 Views · 9 Likes
article thumbnail
How to Compose an Infinispan Docker Image
Read on to explore how to create multicontainer Docker applications involving Infinispan with the help of Docker Compose.
December 7, 2016
by Manik Surtani
· 7,360 Views · 3 Likes
article thumbnail
Top 5 Factors That Impact Your Software Performance
Knowing where your software is failing is essential to identifying the bottleneck. These five performance-impacting factors give you a good place to start.
December 7, 2016
by Thamwika Bergstrom
· 6,962 Views · 3 Likes
article thumbnail
How to Use Compare and Merge for SwaggerHub
Read on to find out how to keep API documentation shipping moving forward smoothly by using Compare and Merge for SwaggerHub.
December 7, 2016
by Keshav Vasudevan
· 8,108 Views · 4 Likes
article thumbnail
Anomaly Detection Using H2O Deep Learning
In this article, we jump straight into creating an anomaly detection model using Deep Learning and anomaly package from H2O.
December 6, 2016
by Sibanjan Das
· 25,785 Views · 7 Likes
article thumbnail
5 Signs That Your REST API Isn't RESTful
The author provides five criteria to help you make the distinction between an API that is RESTful based on the original meaning versus the colloquial meaning of REST.
December 6, 2016
by Robert Brautigam
· 37,810 Views · 43 Likes
article thumbnail
Software-Defined Integration: A New Trend or Just a Fancy Name?
Software-defined everything seems to be everywhere these days. In this article, Olga Annenko discusses software-defined integration and what it should mean to you.
December 5, 2016
by Olga Annenko
· 7,523 Views · 2 Likes
article thumbnail
How to Fix the Remote Code Execution Vulnerability in EJS
Learn how to deal with a vulnerability in Snyk that has to do with EJS and code executions.
December 4, 2016
by Tim Kadlec
· 6,711 Views · 2 Likes
article thumbnail
App Security Is a Stack
App security is not just for developers anymore. Securing apps takes the cooperation of dev, ops, and network teams to identify potential threats and implement architecturally appropriate solutions.
December 2, 2016
by Lori MacVittie
· 9,468 Views · 7 Likes
article thumbnail
A Review of Java Template Engines
In this article, Miro Kopecky provides a thorough review of Java template engines Apache Velocity, Apache FreeMarker, Thymeleaf, and Pebble.
December 2, 2016
by Miro Wengner
· 87,068 Views · 12 Likes
article thumbnail
Automating Application Security in Modern Software Development
It’s irresponsible at every level to ignore the risk of insecure code while doubling-down on anti-virus solutions and firewalls — neither of which protects applications.
November 30, 2016
by Jeff Williams
· 8,683 Views · 3 Likes
article thumbnail
Camel and Kura: Providing Telemetry Data as OPC UA
If you're using an industrial M2M protocol, consider the combined power of Camel and Kura to get your telemetry data squared away as OPC UA.
November 29, 2016
by Jens Reimann
· 6,077 Views · 3 Likes
article thumbnail
RESTful API Authentication Basics
We could all use a refresher on API authentication basics. In this post, Guy Levin provides just that, including how to achieve authentication.
November 29, 2016
by Guy Levin
· 106,170 Views · 34 Likes
article thumbnail
Testing a Node.JS Application Within a Docker Container
Learn how to take advantage of the Docker image layering model to run unit or component tests in a Docker container without polluting the production software.
November 29, 2016
by Nahshon Una-Tsameret
· 57,425 Views · 8 Likes
article thumbnail
SDK Patterns for Accelerating API Integration
In this article, James Higginbotham explains what an SDK is, the differences between SDKs and helper libraries, and four different SDK patterns for API providers.
November 29, 2016
by James Higginbotham
· 5,985 Views · 2 Likes
article thumbnail
Optimum Database Connection Pool Size
Want to figure out the best size for your connection pool? Here's how the math pans out, courtesy of some simple logic.
November 28, 2016
by Anubhav R Mulchandani
· 41,638 Views · 5 Likes
article thumbnail
Logging Docker Containers With AWS Cloudwatch
This post describes how to set up the integration between Docker and AWS and then establish a pipeline of logs from CloudWatch into the ELK Stack.
November 28, 2016
by Daniel Berman
· 10,079 Views · 3 Likes
article thumbnail
A Review of Template Engines: What Next After Velocity?
With Velocity deprecated, let's take a walk through some other popular template engines to see what works best where.
November 28, 2016
by Miro Wengner
· 82,657 Views · 7 Likes
  • Previous
  • ...
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×