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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

LIVE: “Cognitive Databases, Intelligent Data: Unified Infrastructure for Vector Search, AI-Optimized Queries, & Hybrid Workloads" Report

Live Webinar: Exclusive practitioner summit on AI-powered CDN operations and real-world automation strategies

The Latest Java Topics

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
· 7,989 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,182 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,773 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,135 Views · 7 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,632 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,510 Views · 49 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,249 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
· 22,909 Views · 6 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,253 Views · 6 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
· 86,981 Views · 12 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,612 Views · 56 Likes
article thumbnail
7 Steps to Preparing for Java 8 Certification
If you're considering getting your Java skills certified, take a look at what you can do to give yourself the best shot possible.
November 30, 2016
by Serdar Mustaoglu
· 147,540 Views · 35 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,393 Views · 15 Likes
article thumbnail
REST With Java 8
See how you can build your own REST services with Java, Maven, and the Spark Java framework.
Updated November 27, 2016
by Unni Mana
· 51,922 Views · 53 Likes
article thumbnail
Interactive Console Applications in Java
Learn how to make your applications pay more attention to what users are telling them.
November 27, 2016
by Serban Iordache
· 163,405 Views · 45 Likes
article thumbnail
A Post-Processor for Spring Boot
Learn how to make your own post-processor for your Spring Boot needs, allowing you to add to your environment with ease.
November 23, 2016
by Nicolas Fränkel
· 26,732 Views · 7 Likes
article thumbnail
Make Your Own Light Switch With Java and a Raspberry Pi
Looking for an IoT project? Look no further than developing your own switch with the helping hand of a library, some Java, and a Raspberry Pi.
November 23, 2016
by Hemambara Vamsi Kotari
· 19,153 Views · 9 Likes
article thumbnail
Inheriting Javadoc Method Comments
Learn how you can set up your Javadoc method comments in order to automatically — partially or fully — pass on their documentation.
November 22, 2016
by Dustin Marx
· 77,077 Views · 5 Likes
article thumbnail
Control a CNC Machine Using Java or Groovy
Feel like doing your own woodworking, or other, similar work? Take a look at how you can give commands to your own computer numeric control machine.
November 22, 2016
by Igor Suhorukov
· 15,278 Views · 16 Likes
article thumbnail
Persisting Natural Key Entities With Spring Data JPA
Learn a handy trick to persist your natural key entities using Spring Data. See how that compares to surrogate keys and what other tricks that permits.
November 22, 2016
by Dawid Kublik
· 21,668 Views · 5 Likes
  • Previous
  • ...
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • ...
  • 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
×