Category: Java

Java’s Synchronized Keyword in Three Minutes

I wrote this article for SitePoint’s Java channel, where you can find a lot of interesting articles about our programming language. Check it out!

The synchronized keyword can be used to ensure that only one thread at a time executes a particular section of code.
This is a simple way to prevent race conditions, which occur when several threads change shared data at the same time in a way that leads to incorrect results.
With synchronized either entire methods or selected blocks can be single-threaded.

This article requires basic knowledge of Java threads and race conditions.

Continue reading

The Dangers of Race Conditions in Five Minutes

I wrote this article for SitePoint’s Java channel, where you can find a lot of interesting articles about our programming language. Check it out!

A race condition is an undesired property of multithreaded code.
It expresses that the program’s outcome depends on a particular order of operations but that the underlying platform (in the case of Java, the JVM) does not guarantee that order.
As a consequence the outcome is often fluctuating across runs as it depends on how exactly operations from different threads interleave.
In Java, race conditions occur most often when multiple threads share and mutate the same object.

The only prerequisite for this article is a basic knowledge of threads.

Continue reading

Java Thread Class in Five Minutes

I wrote this article for SitePoint’s Java channel, where you can find a lot of interesting articles about our programming language. Check it out!

In a running Java program, all code is executed in threads and within a thread everything happens sequentially, one instruction after another.
When Java (or rather the JVM) launches, it creates one thread for the main method to be executed in.
From there, new threads can be created to execute code in parallel to the main one.
The most basic way to do that is to use the Thread class.

This article does not require any knowledge of multithreaded programming, but you need to be familiar with core Java concepts such as classes and interfaces.

Continue reading

Beyond POJOs – Ten More Ways to Reduce Boilerplate with Lombok

I wrote this article for SitePoint’s Java channel, where you can find a lot of interesting articles about our programming language. Check it out!

Lombok is a great library and its main selling point is how it declutters POJO definitions.
But it is not limited to that use case!
In this article, I will show you six stable and four experimental Lombok features that can make your Java code even cleaner.
They cover many different topics, from logging to accessors and from null safety to utility classes.
But they all have one thing in common: reducing boilerplate to make code easier to read and more expressive.

Continue reading

Declutter Your POJOs with Lombok

I wrote this article for SitePoint’s Java channel, where you can find a lot of interesting articles about our programming language. Check it out!

I have a love/hate relationship with Java.
On one hand, it’s a mature programming language with a diverse number of frameworks and libraries that make development relatively easy.
On the other hand, it’s very verbose and requires writing massive amounts of boilerplate code for common tasks.
The situation got better with the introduction of lambdas and streams in Java 8, but it is still sub-par in some areas, like writing plain old Java objects POJO.
In this post, I’ll show you how to rewrite POJOs in only a few lines of code with Lombok.

Continue reading

© 2017 Brewing Codes

Theme by Anders NorenUp ↑