Fixed potentially thread unsafe initialization.
Change-Id: I047bcd9358a544723603a069faa234c00ba6b757
Showing
2 changed files
with
10 additions
and
7 deletions
... | @@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicInteger; | ... | @@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicInteger; |
8 | */ | 8 | */ |
9 | public final class ApplicationId { | 9 | public final class ApplicationId { |
10 | 10 | ||
11 | - private static AtomicInteger idDispenser; | 11 | + private static final AtomicInteger ID_DISPENCER = new AtomicInteger(1); |
12 | private final Integer id; | 12 | private final Integer id; |
13 | 13 | ||
14 | // Ban public construction | 14 | // Ban public construction |
... | @@ -50,10 +50,7 @@ public final class ApplicationId { | ... | @@ -50,10 +50,7 @@ public final class ApplicationId { |
50 | * @return app id | 50 | * @return app id |
51 | */ | 51 | */ |
52 | public static ApplicationId getAppId() { | 52 | public static ApplicationId getAppId() { |
53 | - if (ApplicationId.idDispenser == null) { | 53 | + return new ApplicationId(ApplicationId.ID_DISPENCER.getAndIncrement()); |
54 | - ApplicationId.idDispenser = new AtomicInteger(1); | ||
55 | - } | ||
56 | - return new ApplicationId(ApplicationId.idDispenser.getAndIncrement()); | ||
57 | } | 54 | } |
58 | 55 | ||
59 | } | 56 | } | ... | ... |
... | @@ -8,7 +8,7 @@ import org.jboss.netty.util.HashedWheelTimer; | ... | @@ -8,7 +8,7 @@ import org.jboss.netty.util.HashedWheelTimer; |
8 | */ | 8 | */ |
9 | public final class Timer { | 9 | public final class Timer { |
10 | 10 | ||
11 | - private static HashedWheelTimer timer; | 11 | + private static volatile HashedWheelTimer timer; |
12 | 12 | ||
13 | // Ban public construction | 13 | // Ban public construction |
14 | private Timer() { | 14 | private Timer() { |
... | @@ -21,10 +21,16 @@ public final class Timer { | ... | @@ -21,10 +21,16 @@ public final class Timer { |
21 | */ | 21 | */ |
22 | public static HashedWheelTimer getTimer() { | 22 | public static HashedWheelTimer getTimer() { |
23 | if (Timer.timer == null) { | 23 | if (Timer.timer == null) { |
24 | + initTimer(); | ||
25 | + } | ||
26 | + return Timer.timer; | ||
27 | + } | ||
28 | + | ||
29 | + private static synchronized void initTimer() { | ||
30 | + if (Timer.timer == null) { | ||
24 | Timer.timer = new HashedWheelTimer(); | 31 | Timer.timer = new HashedWheelTimer(); |
25 | Timer.timer.start(); | 32 | Timer.timer.start(); |
26 | } | 33 | } |
27 | - return Timer.timer; | ||
28 | } | 34 | } |
29 | 35 | ||
30 | } | 36 | } | ... | ... |
-
Please register or login to post a comment