01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.core.util;
15:
16: /**
17: * Clase de utilidad en el ámbito de resolver el problema
18: * del "double-checked locking" (DCL)
19: http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double.html
20: http://www.oreillynet.com/onjava/blog/2007/01/singletons_and_lazy_loading.html
21: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
22: http://en.wikipedia.org/wiki/Double-checked_locking
23: *
24: * En la JVM 1.5 se resuelve fácilmente con "volatile" pero en 1.4 e inferiores
25: * puede resolverse con un ThreadLocal aunque sólo en 1.4 es suficientemente rápido
26: * y no evita la sincronización la primera vez que se chequea
27: * http://www.javaworld.com/javaworld/jw-11-2001/jw-1116-dcl.html?page=3
28: *
29: * @author jmarranz
30: */
31: public class ThreadLocalDCL {
32: protected static final boolean is1_4orLower; // toma un primer valor y no cambia
33:
34: static {
35: String verStr = System.getProperty("java.vm.version");
36: // Ej. java.vm.version=1.5.0_02-b09
37: int pos = verStr.indexOf('.');
38: int firstVer = Integer.parseInt(verStr.substring(0, pos));
39: if (firstVer == 1) {
40: // Versión 1.x
41: verStr = verStr.substring(pos + 1);
42: pos = verStr.indexOf('.');
43: if (pos != -1)
44: verStr = verStr.substring(0, pos);
45: int secondVer = Integer.parseInt(verStr);
46:
47: is1_4orLower = (secondVer <= 4);
48: } else
49: is1_4orLower = false; // Es v2 o superior
50:
51: //int first = verStr.
52: }
53:
54: public static ThreadLocal newThreadLocal() {
55: if (is1_4orLower)
56: return new ThreadLocal();
57: else
58: return null; // No es necesario, usar volatile resuelve el problema
59: }
60: }
|