A new thread enters the running state through Start (), and the yield () method can be used for comity in the running state, but it can still be done, and if the field needs to be paused now, it can be suspend (),
Sleep (), wait () method. If the thread does not execute, it can be ended by ending with a stop () (if the run () method finishes executing) or a new thread calling stop () directly.
Suspend (): Pending.
Resume (): Resumes the pending state.
Stop (): Stops the process.
The above methods are prone to deadlock and are not recommended for use.
What do I do if I want to stop a thread now?
Lets the thread stop running by setting the flag bit. To stop, set a method so that the flag bit is false. As follows:
classMyThreadImplementsrunnable{Private Boolean flag = true; Defining Flag bits Public voidrun () {inti = 0 ; while( This. Flag) {System.out.println (Thread.CurrentThread (). GetName ()+ "Run, I =" + (i++)) ; } } Public void Stop () { this.flag = false; Modify Flag bit }}; Public classstopdemo{ Public Static voidMain (String args[]) {MyThread my=NewMyThread (); Thread T=NewThread (My, "thread");//To create a thread objectT.start ();//Start Thread Try{Thread.Sleep (30) ; }Catch(Exception e) {} my.stop (); //Modify flag bit, stop running }};
Thread's life cycle, thread end