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

article thumbnail
Marshalling and Unmarshalling in JAXB 2.0
Take a look at how to marshal and unmarshal your Java objects and XML data with JAXB 2.0, a useful tool for generating XML schemas from Java code and vice-versa.
December 30, 2016
by Abhishek Giri
· 229,901 Views · 11 Likes
article thumbnail
A Functional Approach to Given When Then Using Java 8
A couple of months ago, while working on a new project, I started to look for an easy way to have this Given-When-Then separation in my tests.
Updated December 29, 2016
by Gabriel Deliu
· 32,180 Views · 19 Likes
article thumbnail
Single vs. Multiple Filters in the Java Stream API
It might be tempting to run multiple filters in your streams, but be careful—it might come with a cost. Use your filters judiciously.
December 29, 2016
by A N M Bazlur Rahman DZone Core CORE
· 116,016 Views · 6 Likes
article thumbnail
Send, read Emails and Appointments From MS Exchange [using Java]
This code will help you connect your app to MS Exchange using the EWS Java API. In its current form, the snippet fetches emails and appointments and can be broadened.
December 27, 2016
by Shantanu Sikdar
· 67,126 Views · 6 Likes
article thumbnail
How to Resolve the ''ActiveMQConnectionFactory Class Not Found'' Exception
This error means that your Mule application is looking for activemq supporting classes in your project and it is not finding any.
December 26, 2016
by Akkiraju Ivaturi
· 26,354 Views · 3 Likes
article thumbnail
Packaging Spring Boot Apps With External Dependencies Using Maven
Incorporate external dependencies for your Spring Boot app with a helping hand from Maven. You can package everything together, making it attractive for microservices.
December 23, 2016
by Harshith Bhaskar
· 186,670 Views · 14 Likes
article thumbnail
Is Standard Java Logging Dead?
Does JUL still have a place in the ecosystem? See how a variety of third-party logging tools stack up against Java's built-in logging power.
December 23, 2016
by Alex Zhitnitsky
· 19,581 Views · 10 Likes
article thumbnail
JDK 9: An Introduction to StackWalker
StackWalker is an API for stack walking that allows easy filtering of, and lazy access to, the information in stack traces.
December 22, 2016
by Hemambara Vamsi Kotari
· 24,692 Views · 24 Likes
article thumbnail
We Don't Need StringBuilder for Simple Concatenation
Concatenating strings is useful, but expensive. Fortunately, you don't need to use StringBuilder anymore - the compiler can handle it for you.
December 22, 2016
by A N M Bazlur Rahman DZone Core CORE
· 197,409 Views · 44 Likes
article thumbnail
What Is Node.js for Java Developers?
Node.js is growing in popularity, but how exactly does it work? This guide breaks down the terminology into Java-friendly terms for easy understanding.
December 22, 2016
by Ratha KM
· 82,964 Views · 57 Likes
article thumbnail
Being Wary Does Not Hurt Java
Being careful is not the same as being hurtful to the larger community. With Oracle's licensing audits still fresh in mind, it's important to stay alert.
December 21, 2016
by Markus Eisele
· 14,299 Views · 19 Likes
article thumbnail
A Look at ScheduledService [Code Snippets]
ScheduledService lets you execute the same task a regular intervals and can even restart itself in the event of a failure, making it a good tool to have in the box.
December 20, 2016
by Arun Pandey DZone Core CORE
· 40,165 Views · 8 Likes
article thumbnail
How to Convert Maven to Gradle and Vice Versa
Converting Maven to Gradle only takes one step, and converting Gradle back to Maven isn't much more difficult.
December 18, 2016
by A N M Bazlur Rahman DZone Core CORE
· 214,690 Views · 18 Likes
article thumbnail
JSON-B: A Java API for JSON Binding
When JSON-B and JSON-P are combined, all of the tools are put in place to process and work with the JSON data format in Java.
December 14, 2016
by Sam Sepassi
· 23,269 Views · 6 Likes
article thumbnail
Deploying Microservices: Spring Cloud vs. Kubernetes
When it comes to deploying microservices, which is better — Spring Cloud or Kubernetes? The answer is both, but they shine in different ways.
December 13, 2016
by Bilgin Ibryam
· 196,804 Views · 104 Likes
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,967 Views · 17 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,411 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,094 Views · 2 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,275 Views · 8 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,946 Views · 12 Likes
  • Previous
  • ...
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • ...
  • 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
×