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 Coding Topics

article thumbnail
How to Build You Own Personal Jenkins CI Server
There are tons of benefits to running a personal CI server, including that it can prioritize your own branches and builds and that you can build and deploy while failing tests.
December 13, 2016
by Matthew Casperson
· 34,351 Views · 1 Like
article thumbnail
About the Java 8 Stream API Bug
The Java Stream API wasn't working the way it was supposed to. There is a fix, but it's interesting to see what exactly went wrong.
December 12, 2016
by A N M Bazlur Rahman DZone Core CORE
· 16,906 Views · 17 Likes
article thumbnail
Building a Simple PubSub System in JavaScript
Ever wanted to build a pub-sub system in JavaScript? Sure you have! In this post, we take a look at how to build a simple one, complete with code. Read on to find out more.
December 12, 2016
by Paul Kinlan
· 13,283 Views · 7 Likes
article thumbnail
Node.js Performance Showdown
Let's pit different frameworks against each other and see who comes out on top.
December 12, 2016
by Alex Ogier
· 12,830 Views · 2 Likes
article thumbnail
Interactive Debugging With Node.js
This post will introduce the Node.js debugging tools in Visual Studio Code (VS Code) for programmers who have never used a debugger before.
December 12, 2016
by Sequoia McDowell
· 14,021 Views · 8 Likes
article thumbnail
6 Frequently Asked Hadoop Interview Questions and Answers
Prepping for your upcoming interview? Unsure about what Hadoop knowledge to take with you? Here are 6 frequently asked Hadoop interview questions and the answers you should be giving.
December 11, 2016
by Arul Kumaran
· 34,377 Views · 15 Likes
article thumbnail
How to Use Asynchronous Timeouts in the Java Websocket API
In this post we take a look at how to deal with timeouts when using the Java WebSocket API. Read on to find out how and for some example code.
December 10, 2016
by Abhishek Gupta DZone Core CORE
· 12,356 Views · 4 Likes
article thumbnail
Improving Alerts With Reverse AJAX (Part 2)
Implementing Reverse AJAX can keep users updated with the latest data without adding an unnecessary load to the server.
December 9, 2016
by Sulthony H
· 8,015 Views · 2 Likes
article thumbnail
Mastering the Couchbase N1QL Shell: Connection Management and Security
Couchbase's cbq shell lets you write and run N1QL queries interactively. The shell also lets you securely interact with mixed nodes, among other handy tricks.
December 9, 2016
by Isha Kandaswamy
· 8,696 Views · 5 Likes
article thumbnail
Declarative Programming With Speedment 3.0
Learn more on the fundamentals of declarative programming in this in-depth article on the concept and see how Speedment implements declarative programming in practice.
December 9, 2016
by Dan Lawesson
· 11,207 Views · 8 Likes
article thumbnail
Create a REST API with Speedment and Spring
You can build a complete REST API with almost no manual coding using open-source Speedment and Spring.
December 9, 2016
by Emil Forslund
· 12,552 Views · 7 Likes
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,830 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,165 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,925 Views · 9 Likes
article thumbnail
Using Web Components in Plain Java
See how you can use Vaadin, as well as a few other tools, to incorporate web components in your Java projects.
December 8, 2016
by Alejandro Duarte DZone Core CORE
· 11,656 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,544 Views · 49 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,365 Views · 3 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,077 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,292 Views · 14 Likes
article thumbnail
IntelliJ IDEA Inspection Settings for Java 8 Refactoring
Follow along as Trisha Gee explains how you can use IntelliJ IDEA to refactor your code for use in Java 8, covering loops, streams, and more.
December 6, 2016
by Trisha Gee
· 23,026 Views · 6 Likes
  • Previous
  • ...
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • ...
  • 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
×