KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > InputDoubleCheckedLocking


1 ////////////////////////////////////////////////////////////////////////////////
2
// Test case file for checkstyle.
3
// Created: 2001
4
////////////////////////////////////////////////////////////////////////////////
5
package com.puppycrawl.tools.checkstyle;
6
7 /**
8  * Test case for detection of double checked locking
9  * @author lkuehne
10  **/

11 class InputDoubleCheckedLocking
12 {
13     static Integer JavaDoc one = null;
14
15     private static Integer JavaDoc getOneCorrect()
16     {
17         synchronized (InputDoubleCheckedLocking.class)
18         {
19             if (one == null)
20             {
21                 one = new Integer JavaDoc(1);
22             }
23         }
24         return one;
25     }
26
27     private static Integer JavaDoc getOneDCL()
28     {
29         if (one == null)
30         {
31             System.out.println("just to make the AST interesting");
32             synchronized (InputDoubleCheckedLocking.class)
33             {
34                 if (one == null)
35                 {
36                     one = new Integer JavaDoc(1);
37                 }
38             }
39         }
40         return one;
41     }
42
43     private static Integer JavaDoc getSimilarToDCL()
44     {
45         // different tests
46
if (one == null)
47         {
48             synchronized (InputDoubleCheckedLocking.class)
49             {
50                 if (one == Integer.valueOf(2))
51                 {
52                     one = new Integer JavaDoc(1);
53                 }
54             }
55         }
56
57         // no synchronization
58
if (one == null)
59         {
60             if (one == null)
61             {
62                 one = new Integer JavaDoc(1);
63             }
64         }
65
66         // no outer test
67
synchronized (InputDoubleCheckedLocking.class)
68         {
69             if (one == null)
70             {
71                 one = new Integer JavaDoc(1);
72             }
73         }
74         return one;
75     }
76
77 }
78
Popular Tags