Permalink
Browse files

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...
1 parent dd7db22 commit c4067cdc3b9ee23687486d09da1865b08709278a @jonalv jonalv committed with egonw Aug 23, 2011
Showing with 5 additions and 4 deletions.
  1. +5 −4 src/main/org/openscience/cdk/tools/periodictable/PeriodicTable.java
@@ -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
*
@@ -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;

0 comments on commit c4067cd

Please sign in to comment.