1111import java .util .List ;
1212import java .util .Map ;
1313import java .util .concurrent .*;
14+ import java .util .concurrent .atomic .AtomicReference ;
1415
1516public class ExecutorsScheduler implements Scheduler {
1617
@@ -28,17 +29,33 @@ public ExecutorsScheduler(Plugin pluginBukkit) {
2829 }
2930
3031 @ Override
31- public @ Nullable SchedulerTaskInter runAtFixedRate (@ NotNull SchedulerType schedulerType , SchedulerCallBack callBack , long initialDelayTicks , long periodTicks ) {
32+ public SchedulerTaskInter runAtFixedRate (@ NotNull SchedulerType schedulerType , SchedulerCallBack callBack , long initialDelayTicks , long periodTicks ) {
3233 if (!schedulerType .equals (SchedulerType .ASYNC )) {
3334 throw new UnsupportedOperationException ();
3435 }
36+ final AtomicReference <ExecutorsSchedulerTask > executorsSchedulerTaskRef = new AtomicReference <>();
37+
3538 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
36- executorService . scheduleAtFixedRate (() -> {
37- ExecutorsSchedulerTask executorsScheduler = new ExecutorsSchedulerTask ( plugin , executorService );
39+
40+ TaskRunnable taskRunnable = ( executorsScheduler ) -> {
3841 mapSchedulerTask .put (executorsScheduler .getTaskId (), executorsScheduler );
3942 callBack .run (executorsScheduler );
43+ };
44+
45+ executorsSchedulerTaskRef .set (new ExecutorsSchedulerTask (this .plugin , executorService ));
46+
47+ executorService .scheduleAtFixedRate (() -> {
48+ taskRunnable .run (executorsSchedulerTaskRef .get ());
4049 }, initialDelayTicks * 50 , periodTicks * 50 , TimeUnit .MILLISECONDS );
41- return new ExecutorsSchedulerTask (plugin , executorService );
50+
51+ executorService .shutdown ();
52+ try {
53+ executorService .awaitTermination (60 , TimeUnit .SECONDS );
54+ } catch (InterruptedException e ) {
55+ Thread .currentThread ().interrupt ();
56+ }
57+
58+ return executorsSchedulerTaskRef .get ();
4259 }
4360
4461 @ Override
@@ -61,13 +78,29 @@ public ExecutorsScheduler(Plugin pluginBukkit) {
6178 if (!schedulerType .equals (SchedulerType .ASYNC )) {
6279 throw new UnsupportedOperationException ();
6380 }
81+ final AtomicReference <ExecutorsSchedulerTask > executorsSchedulerTaskRef = new AtomicReference <>();
82+
6483 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
65- executorService . schedule (() -> {
66- ExecutorsSchedulerTask executorsScheduler = new ExecutorsSchedulerTask ( plugin , executorService );
84+
85+ TaskRunnable taskRunnable = ( executorsScheduler ) -> {
6786 mapSchedulerTask .put (executorsScheduler .getTaskId (), executorsScheduler );
6887 callBack .run (executorsScheduler );
88+ };
89+
90+ executorsSchedulerTaskRef .set (new ExecutorsSchedulerTask (this .plugin , executorService ));
91+
92+ executorService .schedule (() -> {
93+ taskRunnable .run (executorsSchedulerTaskRef .get ());
6994 }, delayTicks * 50 , TimeUnit .MILLISECONDS );
70- return new ExecutorsSchedulerTask (plugin , executorService );
95+
96+ executorService .shutdown ();
97+ try {
98+ executorService .awaitTermination (60 , TimeUnit .SECONDS );
99+ } catch (InterruptedException e ) {
100+ Thread .currentThread ().interrupt ();
101+ }
102+
103+ return executorsSchedulerTaskRef .get ();
71104 }
72105
73106 @ Override
@@ -90,13 +123,29 @@ public ExecutorsScheduler(Plugin pluginBukkit) {
90123 if (!schedulerType .equals (SchedulerType .ASYNC )) {
91124 throw new UnsupportedOperationException ();
92125 }
126+ final AtomicReference <ExecutorsSchedulerTask > executorsSchedulerTaskRef = new AtomicReference <>();
127+
93128 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
94- executorService . execute (() -> {
95- ExecutorsSchedulerTask executorsScheduler = new ExecutorsSchedulerTask ( plugin , executorService );
129+
130+ TaskRunnable taskRunnable = ( executorsScheduler ) -> {
96131 mapSchedulerTask .put (executorsScheduler .getTaskId (), executorsScheduler );
97132 callBack .run (executorsScheduler );
133+ };
134+
135+ executorsSchedulerTaskRef .set (new ExecutorsSchedulerTask (this .plugin , executorService ));
136+
137+ executorService .execute (() -> {
138+ taskRunnable .run (executorsSchedulerTaskRef .get ());
98139 });
99- return new ExecutorsSchedulerTask (plugin , executorService );
140+
141+ executorService .shutdown ();
142+ try {
143+ executorService .awaitTermination (60 , TimeUnit .SECONDS );
144+ } catch (InterruptedException e ) {
145+ Thread .currentThread ().interrupt ();
146+ }
147+
148+ return executorsSchedulerTaskRef .get ();
100149 }
101150
102151 @ Override
@@ -169,47 +218,56 @@ public int scheduleSyncRepeating(@NotNull SchedulerType schedulerType, Entity en
169218 throw new UnsupportedOperationException ();
170219 }
171220
172- @ Override @ Deprecated
221+ @ Override
222+ @ Deprecated
173223 public void runAtFixedRate (@ NotNull SchedulerType schedulerType , long initialDelayTicks , long periodTicks , SchedulerCallBack callBack ) {
174224 this .runAtFixedRate (schedulerType , callBack , initialDelayTicks , periodTicks );
175225 }
176226
177- @ Override @ Deprecated
227+ @ Override
228+ @ Deprecated
178229 public void runAtFixedRate (@ NotNull SchedulerType schedulerType , @ Nullable Object chunkOrLoc , long initialDelayTicks , long periodTicks , SchedulerCallBack callBack ) {
179230 throw new UnsupportedOperationException ();
180231 }
181232
182- @ Override @ Deprecated
233+ @ Override
234+ @ Deprecated
183235 public void runAtFixedRate (@ NotNull SchedulerType schedulerType , @ Nullable Object chunkOrLocOrEntity , @ Nullable Runnable retired , long initialDelayTicks , long periodTicks , SchedulerCallBack callBack ) {
184236 throw new UnsupportedOperationException ();
185237 }
186238
187- @ Override @ Deprecated
239+ @ Override
240+ @ Deprecated
188241 public void runDelayed (@ NotNull SchedulerType schedulerType , long delayTicks , SchedulerCallBack callBack ) {
189242 this .runDelayed (schedulerType , callBack , delayTicks );
190243 }
191244
192- @ Override @ Deprecated
245+ @ Override
246+ @ Deprecated
193247 public void runDelayed (@ NotNull SchedulerType schedulerType , @ Nullable Object chunkOrLoc , long delayTicks , SchedulerCallBack callBack ) {
194248 throw new UnsupportedOperationException ();
195249 }
196250
197- @ Override @ Deprecated
251+ @ Override
252+ @ Deprecated
198253 public void runDelayed (@ NotNull SchedulerType schedulerType , @ Nullable Object chunkOrLocOrEntity , @ Nullable Runnable retired , long delayTicks , SchedulerCallBack callBack ) {
199254 throw new UnsupportedOperationException ();
200255 }
201256
202- @ Override @ Deprecated
257+ @ Override
258+ @ Deprecated
203259 public void execute (@ NotNull SchedulerType schedulerType , SchedulerCallBack callBack ) {
204260 this .runTask (schedulerType , callBack );
205261 }
206262
207- @ Override @ Deprecated
263+ @ Override
264+ @ Deprecated
208265 public void execute (@ NotNull SchedulerType schedulerType , @ Nullable Object chunkOrLoc , SchedulerCallBack callBack ) {
209266 throw new UnsupportedOperationException ();
210267 }
211268
212- @ Override @ Deprecated
269+ @ Override
270+ @ Deprecated
213271 public void execute (@ NotNull SchedulerType schedulerType , @ Nullable Object chunkOrLocOrEntity , @ Nullable Runnable retired , SchedulerCallBack callBack ) {
214272 throw new UnsupportedOperationException ();
215273 }
0 commit comments