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() method of the thread. All is good there, but now I want to add a button to simulate cashiers logging off. I added a boolean variable to my while loop onDuty and a public function logOff() which sets this onDuty bool to false to get me out of the run's while loop. My problem now is from my gui class how can I call a function on a specific thread? Each cashier thread has been generated at runtime so I don't know their names.
I hope I made sense. Thanks in advance.
HashMap<String, Thread>
; you mention something about them having "names" which would be the (String
) keys. – Brian RoachThread myThread; Cahier (String name) { myThread = new Thread( this ); myThread.start(); }
– mikeyPmyThread
is your reference - but you also need to set the name of theThread
explicitly by callingsetName(String name)
method on yourThread
reference for each thread you create. Then add the two ( the name and the reference ) into a HashMap. Use theName
as your handle to get your thread later to stop the particular thread. – Bhaskar