Synchronization in Java: synchronized and volatile
synchronized gives mutual exclusion and visibility. volatile gives visibility only, and knowing the difference prevents most concurrency bugs.
Notes related to the Java language.
100 notes tagged
synchronized gives mutual exclusion and visibility. volatile gives visibility only, and knowing the difference prevents most concurrency bugs.
A thread moves through six states, and knowing which one it is in is the first step in diagnosing any hang.
A thread is an independent path of execution inside one process, and Java gives you several ways to start one.
Serialization turns an object graph into bytes and back. It is easy to switch on, hard to maintain, and a genuine security risk.
Every unbuffered read is a system call. Buffering turns thousands of them into a handful, and it is the single largest I/O win available.
InputStream and OutputStream carry raw bytes; Reader and Writer carry characters. Choosing wrongly corrupts data.
Path names a location and Files performs the operations. Together they replaced the older File class for almost every purpose.
DateTimeFormatter converts between text and date time values, and ZoneId handles the region rules that make time zones difficult.
Instant marks a point on the timeline; Duration measures time based amounts and Period measures date based ones.
The modern date and time API is immutable, thread safe and clearly named, and it replaced the older Date and Calendar classes entirely.
A parallel stream splits the work across threads. It helps less often than people expect, and it breaks silently when the rules are ignored.
Collectors turn a stream into a collection, a map or a summary, and grouping is where they become genuinely powerful.