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 Data Engineering 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,804 Views · 12 Likes
article thumbnail
Excel Hacks To Ignore Missing Data
Is missing data an obstacle for you? Here are some workarounds in Excel so you can move past it.
December 8, 2016
by B Jones
· 7,914 Views · 3 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,896 Views · 9 Likes
article thumbnail
Creating Maps With Named Lambdas
Learn how you can create a Java Map like this: map = mapOf(one -> 1, two -> 2) using a trick to get the lambda parameter name.
December 7, 2016
by Per-Åke Minborg
· 23,525 Views · 49 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,946 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,094 Views · 4 Likes
article thumbnail
Installing RavenDB 4.0 on Your Raspberry Pi 3
Here's a quick guide to get the NoSQL database RavenDB up and running on your Raspberry Pi — a possible data solution for your IoT projects.
December 7, 2016
by Oren Eini
· 7,044 Views · 2 Likes
article thumbnail
Apache Ignite With JPA: A Missing Element
Learn how to persist your entities with Apache Ignite and JPA. This tutorial will guide you through the setup of execution of that handy ability.
December 7, 2016
by Shamim Bhuiyan
· 15,264 Views · 14 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,769 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,790 Views · 43 Likes
article thumbnail
Java Holiday Calendar 2016 (Day 5): CRUD Operations
See how, with a handy open-source tool, you can alter your database entities, with standard CRUD operations, in Java.
December 5, 2016
by Per-Åke Minborg
· 9,262 Views · 6 Likes
article thumbnail
How to Make Your Own Web Spreadsheet App: Tools Review
Read on for a thorough review and comparison of the spreadsheet apps Webex, Kendo UI, and Handsontable.
December 5, 2016
by Ivan Petrenko
· 12,619 Views · 3 Likes
article thumbnail
Keeping Submodules Up to Date in Git
When your project gets complicated, it makes sense to split it up into manageable chunks. Git supports this process using submodules.
December 5, 2016
by Tim Myerscough
· 9,514 Views · 2 Likes
article thumbnail
Benchmarking NATS Streaming and Apache Kafka
In this article, Tyler Treat looks at NATS Streaming and Apache Kafka, compares features of both, and quantifies their performance characteristics through benchmarking.
December 5, 2016
by Tyler Treat
· 63,490 Views · 10 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,505 Views · 2 Likes
article thumbnail
Spring JdbcTemplate: RowMapper vs. ResultSetExtractor
In this comparison, see when RowMapper and ResultSetExtractor work better when you're diving into the JdbcTemplate class.
December 1, 2016
by Stoyan Mitov
· 196,582 Views · 7 Likes
article thumbnail
Multi-Tenancy Using JPA, Spring, and Hibernate (Part 1)
Learn how you can make your application act like multiple, independent apps by implementing multi-tenancy and keeping your data accessible by the tenants.
December 1, 2016
by Jose Manuel García Maestre
· 91,640 Views · 56 Likes
article thumbnail
More Than LIKE: Efficient JSON Searching With N1QL
See how the latest iteration of Couchbase allows you to split and search JSON data with tokens.
November 30, 2016
by Keshav Murthy DZone Core CORE
· 12,460 Views · 6 Likes
article thumbnail
When to Use (and Not to Use) MongoDB
If you've been considering a jump to NoSQL, here's an overview of MongoDB and how it can help you and your work.
November 30, 2016
by Prashanth Jayaram
· 190,943 Views · 23 Likes
article thumbnail
Streams in Hibernate and Beyond
Read this post and learn how to take the first steps away from imperative database programming in Java using Hibernate and then how to go fully declarative with the Speedment stream ORM.
November 30, 2016
by Dan Lawesson
· 24,410 Views · 15 Likes
  • Previous
  • ...
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • ...
  • 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
×