1111public class Loop extends Thread {
1212
1313 private final Logger logger ;
14- private volatile boolean running = false ;
1514 private final LongSupplier loopBody ;
15+ private final Queue <Runnable > runNextQueue ;
1616
17+ private volatile boolean running ;
1718 private volatile Runnable onCloseCb ;
1819 private volatile ToLongFunction <Throwable > onErrorCb ;
19- private final Queue <Runnable > runNextQueue ;
2020
2121 public Loop (String name , long sleep , Runnable target ) {
2222 super (name );
@@ -31,28 +31,28 @@ public Loop(String name, long sleep, Runnable target) {
3131 }
3232
3333 public void onClose (Runnable callback ) {
34- if (this . onCloseCb != null ) {
34+ if (onCloseCb != null ) {
3535 Runnable oldCallback = this .onCloseCb ;
36- this . onCloseCb = () -> {
36+ onCloseCb = () -> {
3737 oldCallback .run ();
3838 callback .run ();
3939 };
4040 } else {
41- this . onCloseCb = callback ;
41+ onCloseCb = callback ;
4242 }
4343 }
4444
4545 public void onError (ToLongFunction <Throwable > callback ) {
46- if (this . onErrorCb == null ) {
47- this . onErrorCb = callback ;
46+ if (onErrorCb == null ) {
47+ onErrorCb = callback ;
4848 return ;
4949 }
5050
5151 // Adding a second handler will only be invoked if the
5252 // previous one throws (or rethrows) the incoming exception.
5353 // The callback is invoked with the rethrown exception.
5454 ToLongFunction <Throwable > oldCallback = this .onErrorCb ;
55- this . onErrorCb = t -> {
55+ onErrorCb = t -> {
5656 try {
5757 return oldCallback .applyAsLong (t );
5858 } catch (Throwable tPrime ) {
@@ -68,32 +68,34 @@ public void doNext(Runnable next) {
6868 @ Override
6969 public void run () {
7070 Runnable next ;
71- while (this . running ) {
71+ while (running ) {
7272 long sleep ;
7373 try {
7474 // Run the loop body
7575 sleep = this .loopBody .getAsLong ();
7676
7777 // Run all queued tasks
78- while ((next = this .runNextQueue .poll ()) != null )
78+ while ((next = this .runNextQueue .poll ()) != null ) {
7979 next .run ();
80+ }
8081 } catch (Throwable t ) {
81- if (this .onErrorCb != null )
82+ if (this .onErrorCb != null ) {
8283 sleep = this .onErrorCb .applyAsLong (t );
83- else
84+ } else {
8485 throw t ;
86+ }
8587 }
8688
8789 if (sleep > 0 ) {
8890 try {
8991 Thread .sleep (sleep );
9092 } catch (InterruptedException e ) {
9193 logger .debug ("Loop interrupted. Stopping..." );
92- this . running = false ;
94+ running = false ;
9395 }
9496 } else if (sleep < 0 ) {
9597 logger .debug ("Loop interrupted by a negative sleep request. Stopping..." );
96- this . running = false ;
98+ running = false ;
9799 }
98100 }
99101
0 commit comments