diff --git a/twin/src/main/java/com/iluwatar/twin/BallThread.java b/twin/src/main/java/com/iluwatar/twin/BallThread.java index 9d4d9cf71a76..4e5270ce8bf4 100644 --- a/twin/src/main/java/com/iluwatar/twin/BallThread.java +++ b/twin/src/main/java/com/iluwatar/twin/BallThread.java @@ -24,55 +24,76 @@ */ package com.iluwatar.twin; -import lombok.Setter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import lombok.extern.slf4j.Slf4j; /** - * This class is a UI thread for drawing the {@link BallItem}, and provide the method for suspend + * This class is a UI thread for drawing the {@link BallItem}, and provides methods for suspend * and resume. It holds the reference of {@link BallItem} to delegate the draw task. */ @Slf4j public class BallThread extends Thread { - @Setter - private BallItem twin; - - private volatile boolean isSuspended; + private static final Logger LOGGER = LoggerFactory.getLogger(BallThread.class); private volatile boolean isRunning = true; + private volatile boolean isSuspended = false; + private final Object lock = new Object(); + private final BallItem twin; - /** - * Run the thread. - */ - public void run() { + public BallThread(BallItem twin) { + this.twin = twin; + } - while (isRunning) { - if (!isSuspended) { - twin.draw(); - twin.move(); - } - try { - Thread.sleep(250); - } catch (InterruptedException e) { - throw new RuntimeException(e); + @Override + public void run() { + try { + while (isRunning) { + if (isSuspended) { + synchronized (lock) { + lock.wait(); + } + } else { + twin.doDraw(); + twin.move(); + Thread.sleep(250); + } } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); } } + /** + * Suspend the thread. + */ public void suspendMe() { isSuspended = true; LOGGER.info("Begin to suspend BallThread"); } + /** + * Notify run to resume. + */ public void resumeMe() { isSuspended = false; LOGGER.info("Begin to resume BallThread"); + synchronized (lock) { + lock.notifyAll(); + } } + /** + * Stop running thread. + */ public void stopMe() { this.isRunning = false; this.isSuspended = true; + synchronized (lock) { + lock.notifyAll(); + } } } -