Stopping a specific java thread

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.

Leave a Comment