I have a button \"addCashier\" which is creating a thread called \"Cashier\" now this thread is just simply generating orders every 4 seconds, a while(true) loop in the run(
Thread t = CashierThread(); //keep the reference to thread somewhere...
Now instead of a boolean property use built-in interrupted flag:
public void run() {
while(!Thread.currentThread().isInterrupted()) {
//...
}
}
When you want to turn of the thread by clicking on a button simply call:
t.interrupt();
Of course you need to have access to t
variable from the client code.