Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
thread safety
* Made the static instances volatile so all threads needs to read their state before accessing them
* Made the initialize method synchronized so threads have to wait for the first method to finish inialisation before attempting and finding it already initialized.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
jonalv authored and egonw committed Aug 23, 2011
1 parent dd7db22 commit c4067cd
Showing 1 changed file with 5 additions and 4 deletions.
@@ -1,6 +1,7 @@
/* $Revision: 9167 $ $Author: rajarshi $ $Date: 2007-10-22 01:26:11 +0200 (Mon, 22 Oct 2007) $
*
* Copyright (C) 2008 Rajarshi Guha <rajarshi@users.sf.net>
* 2011 Jonathan Alvarsson <jonalv@users.sf.net>
*
* Contact: cdk-devel@lists.sf.net
*
Expand Down Expand Up @@ -52,11 +53,11 @@
@TestClass("org.openscience.cdk.tools.periodictable.PeriodicTableTest")
public class PeriodicTable {

private static boolean isInitialized = false;
private static Map<String, PeriodicTableElement> elements;
private static Map<Integer, PeriodicTableElement> elementsByNumber;
private static volatile boolean isInitialized = false;
private static volatile Map<String, PeriodicTableElement> elements;
private static volatile Map<Integer, PeriodicTableElement> elementsByNumber;

private static void initialize() {
private synchronized static void initialize() {
if (isInitialized) return;

ElementPTFactory factory;
Expand Down

0 comments on commit c4067cd

Please sign in to comment.