import static java.lang.Thread.currentThread;
import java.util.concurrent.TimeUnit;
class Play implements Runnable {
private volatile boolean stop = false;
public void run() {
while(!stop) {
System.out.println("Play thread running.....");
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Play thread resumed.....");
}
System.out.println("Play thread stopped");
}
public void stop() {
stop = true;
}
}
public class MyClass {
public static void main(String args[]) throws InterruptedException {
Play game = new Play();
Thread tr1 = new Thread(game, "TR1");
tr1.start();
System.out.println(currentThread().getName() + " stop the thread");
game.stop();
TimeUnit.MILLISECONDS.sleep(300);
System.out.println(currentThread().getName() + " end");
}
}
/*
run:
Play thread running.....
main stop the thread
Play thread resumed.....
Play thread stopped
main end
*/