Stopping a specific java thread

后端 未结 4 1691
猫巷女王i
猫巷女王i 2020-12-03 02:07

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(

4条回答
  •  忘掉有多难
    2020-12-03 03:03

    You can keep a reference of the Thread object somewhere so that u can call threadObj.logOff()

    If you are not willing to do that then while creating a thred u can assign a unique name to the thread.

    public void run ()
    {
        this.setName(strCashireID);
        ......
    }
    

    At runtime, u can get the thread by:

    Thread getThread(String strCashireID) {
       ThreadGroup threadGroup = Thread.currentThread( ).getThreadGroup( );
       Threads[] threads = new Thread[ threadGroup.activeCount() ];
       threadGroup.enumerate(threads);
       for (int nIndex=0; nIndex

    I'll still suggest that u store the thread objects in a hash map instead of enumerating them at runtime.

提交回复
热议问题