tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 1 | package org.onlab.util; |
| 2 | |
| 3 | import org.jboss.netty.util.HashedWheelTimer; |
| 4 | |
| 5 | /** |
| 6 | * Hashed-wheel timer singleton. Care must be taken to shutdown the timer |
| 7 | * only when the VM is ready to exit. |
| 8 | */ |
| 9 | public final class Timer { |
| 10 | |
Yuta HIGUCHI | f9ce497 | 2014-10-04 22:13:47 -0700 | [diff] [blame] | 11 | private static volatile HashedWheelTimer timer; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 12 | |
| 13 | // Ban public construction |
| 14 | private Timer() { |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Returns the singleton hashed-wheel timer. |
| 19 | * |
| 20 | * @return hashed-wheel timer |
| 21 | */ |
| 22 | public static HashedWheelTimer getTimer() { |
| 23 | if (Timer.timer == null) { |
Yuta HIGUCHI | f9ce497 | 2014-10-04 22:13:47 -0700 | [diff] [blame] | 24 | initTimer(); |
| 25 | } |
| 26 | return Timer.timer; |
| 27 | } |
| 28 | |
| 29 | private static synchronized void initTimer() { |
| 30 | if (Timer.timer == null) { |
Yuta HIGUCHI | a546afc | 2014-10-06 21:44:48 -0700 | [diff] [blame] | 31 | HashedWheelTimer hwTimer = new HashedWheelTimer(); |
| 32 | hwTimer.start(); |
| 33 | Timer.timer = hwTimer; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 34 | } |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } |