4444import de .bluecolored .bluemap .core .world .World ;
4545
4646import java .io .*;
47- import java .util .Collection ;
48- import java .util .Map ;
49- import java .util .UUID ;
47+ import java .util .*;
5048import java .util .concurrent .TimeUnit ;
5149import java .util .zip .GZIPInputStream ;
5250import java .util .zip .GZIPOutputStream ;
@@ -58,8 +56,8 @@ public class Plugin {
5856
5957 private final InterruptableReentrantLock loadingLock = new InterruptableReentrantLock ();
6058
61- private MinecraftVersion minecraftVersion ;
62- private String implementationType ;
59+ private final MinecraftVersion minecraftVersion ;
60+ private final String implementationType ;
6361 private ServerInterface serverInterface ;
6462
6563 private BlueMapService blueMap ;
@@ -70,8 +68,10 @@ public class Plugin {
7068
7169 private RenderManager renderManager ;
7270 private WebServer webServer ;
73- private Thread periodicalSaveThread ;
74- private Thread metricsThread ;
71+
72+ private final Timer daemonTimer ;
73+ private TimerTask periodicalSaveTask ;
74+ private TimerTask metricsTask ;
7575
7676 private PluginConfig pluginConfig ;
7777 private MapUpdateHandler updateHandler ;
@@ -83,6 +83,8 @@ public Plugin(MinecraftVersion minecraftVersion, String implementationType, Serv
8383 this .minecraftVersion = minecraftVersion ;
8484 this .implementationType = implementationType .toLowerCase ();
8585 this .serverInterface = serverInterface ;
86+
87+ this .daemonTimer = new Timer ("BlueMap-Daemon-Timer" , true );
8688 }
8789
8890 public void load () throws IOException , ParseResourceException {
@@ -167,28 +169,26 @@ public void load() throws IOException, ParseResourceException {
167169 Logger .global .logError ("Failed to load render-manager state!" , ex );
168170 }
169171
170- //create periodical-save thread
171- periodicalSaveThread = new Thread (() -> {
172- try {
173- while (true ) {
174- Thread .sleep (TimeUnit .MINUTES .toMillis (5 ));
175- try {
176- saveRenderManagerState ();
177-
178- //clean up caches
179- for (World world : blueMap .getWorlds ().values ()) {
180- world .cleanUpChunkCache ();
181- }
182- } catch (IOException ex ) {
183- Logger .global .logError ("Failed to save render-manager state!" , ex );
172+ //do periodical saves
173+ periodicalSaveTask = new TimerTask () {
174+ @ Override
175+ public void run () {
176+ try {
177+ saveRenderManagerState ();
178+
179+ //clean up caches
180+ for (World world : blueMap .getWorlds ().values ()) {
181+ world .cleanUpChunkCache ();
184182 }
183+ } catch (IOException ex ) {
184+ Logger .global .logError ("Failed to save render-manager state!" , ex );
185+ } catch (InterruptedException ex ) {
186+ this .cancel ();
187+ Thread .currentThread ().interrupt ();
185188 }
186- } catch (InterruptedException ex ){
187- Thread .currentThread ().interrupt ();
188- return ;
189189 }
190- }) ;
191- periodicalSaveThread . start ( );
190+ };
191+ daemonTimer . schedule ( periodicalSaveTask , TimeUnit . MINUTES . toMillis ( 5 ), TimeUnit . MINUTES . toMillis ( 5 ) );
192192
193193 //start map updater
194194 this .updateHandler = new MapUpdateHandler (this );
@@ -208,21 +208,14 @@ public void load() throws IOException, ParseResourceException {
208208 }
209209
210210 //metrics
211- metricsThread = new Thread (() -> {
212- try {
213- Thread .sleep (TimeUnit .MINUTES .toMillis (1 ));
214-
215- while (true ) {
216- if (serverInterface .isMetricsEnabled (coreConfig .isMetricsEnabled ())) Metrics .sendReport (this .implementationType );
217-
218- Thread .sleep (TimeUnit .MINUTES .toMillis (30 ));
219- }
220- } catch (InterruptedException ex ){
221- Thread .currentThread ().interrupt ();
222- return ;
211+ metricsTask = new TimerTask () {
212+ @ Override
213+ public void run () {
214+ if (Plugin .this .serverInterface .isMetricsEnabled (coreConfig .isMetricsEnabled ()))
215+ Metrics .sendReport (Plugin .this .implementationType );
223216 }
224- }) ;
225- metricsThread . start ( );
217+ };
218+ daemonTimer . scheduleAtFixedRate ( metricsTask , TimeUnit . MINUTES . toMillis ( 1 ), TimeUnit . MINUTES . toMillis ( 30 ) );
226219
227220 loaded = true ;
228221
@@ -252,19 +245,8 @@ public void unload() {
252245 serverInterface .unregisterAllListeners ();
253246
254247 //stop scheduled threads
255- if (metricsThread != null ) {
256- metricsThread .interrupt ();
257- try {metricsThread .join (1000 );} catch (InterruptedException ignore ) { Thread .currentThread ().interrupt (); }
258- if (metricsThread .isAlive ()) Logger .global .logWarning ("The metricsThread did not terminate correctly in time!" );
259- metricsThread = null ;
260- }
261-
262- if (periodicalSaveThread != null ) {
263- periodicalSaveThread .interrupt ();
264- try {periodicalSaveThread .join (1000 );} catch (InterruptedException ignore ) { Thread .currentThread ().interrupt (); }
265- if (periodicalSaveThread .isAlive ()) Logger .global .logWarning ("The periodicalSaveThread did not terminate correctly in time!" );
266- periodicalSaveThread = null ;
267- }
248+ metricsTask .cancel ();
249+ periodicalSaveTask .cancel ();
268250
269251 //stop services
270252 if (renderManager != null ) renderManager .stop ();
0 commit comments