Posted in Uncategorized

Concurrency in Java

1. Processes and Threads

A Java program runs in its own process. Java supports within one program the usage of threads.
Java supports threads as part of the Java language. Java 1.5 also provides improved support for concurrency with the in the package java.util.concurrent.
Java also provides locks to protect certain parts of the coding to be executed by several threads at the same time. The simplest way of locking a certain method or Java class is to use the keyword “synchronized” in a method or class declaration.

2. Synchronized

The synchronized keyword in Java ensures:

  • that only a single thread can execute a block of code at the same time
  • ensures that each thread entering a synchronized block of code sees the effects of all previous modifications that were guarded by the same lock

Synchronization is necessary for mutual exclusive access of code blocks and for reliable communication between threads.

synchronized can be used directly in the definition of a method. This would ensure that only one thread can access this method. Or you can used synchronized blocks which a named block (either a string or a object). Then all code which is protected by the same lock can only be executed by one thread.
For example the following datastructure will ensure that only one thread can access the inner block of add() and next()

package de.vogella.pagerank.crawler;

import java.util.ArrayList;
import java.util.List;

* Data structure for a web crawler. Keeps track of the visited sites and keeps
* a list of sites which needs still to be crawled.
* @author Lars Vogel

public class CrawledSites {
private List<String> crawledSites = new ArrayList<String>();
private List<String> linkedSites = new ArrayList<String>();

public void add(String site) {
synchronized (this) {
if (!crawledSites.contains(site)) {

* Get next site to crawl. Can return null (if nothing to crawl)

public String next() {
if (linkedSites.size() == 0) {
return null;
synchronized (this) {
// Need to check again if size has changed
if (linkedSites.size() > 0) {
String s = linkedSites.get(0);
return s;
return null;


3. Volatile

If a variable is declared as volatile then is guaranteed that any thread which reads the field will see the most recently written value. The keyword volatile will not perform any mutual exclusive lock on the variable.
As of Java 5 write access to a volatile variable will also update non-volatile variables which were modified by the same thread. This can also be used to update values within a reference variable, e.g. for a volatile variable person. In this case you must use a temporary variable person and use the setter to initialize the variable and then assign the temporary variable to the final variable. This will then make the address changes of this variable and the values visible to other threads.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s