Basic threading control can be done through threading methods, in addition to the known start, run, and sleep methods, as well as IsAlive, CurrentThread, and interrupt methods.
IsAlive: This method is used to test whether the thread is active. After the thread is started by the Start method, it is in the alive state at any point between termination. The method returns False when it is in a new state and a dead state.
CurrentThread: The method is a class method of the thread class that returns the thread that is using the CPU resource.
Interrupt: When a thread is in hibernation, a thread that occupies a CPU resource can let the dormant thread invoke the interrupt method to wake itself up, causing the dormant thread to interruptedexception an abnormal end to hibernate, and re-queue for CPU resources.
classAImplementsrunnable{Thread Student,teacher; A () {teacher=NewThread ( This, "Professor Wang"); Student=NewThread ( This, "Zhang San"); } Public voidrun () {if(Thread.CurrentThread () = =student) { Try{System.out.println (Student.getname ()+ "sleeping, no lectures"); Thread.Sleep (1000*60*60); }Catch(interruptedexception e) {System.out.println (Student.getname ()+ "Woke up by the teacher"); } } Else if(Thread.CurrentThread () = =teacher) { for(inti=1;i<=3;i++) {System.out.println (Class "); }Try{Thread.Sleep (500); }Catch(Interruptedexception e) {} student.interrupt (); } }} Public classbasiccontrolthread{ Public Static voidMain (String args[]) {A A=NewA (); A.student.start (); A.teacher.start (); }}
Also there are stop and join methods
Stop (): Terminates the thread by calling the thread's instance method stop (), which, after termination, enters the dead state and can no longer be dispatched.
Join (): A thread can have other threads call the Join () method and this thread union while occupying CPU resources. The current thread waits for the method thread to call the end, and then re-queues the CPU resources to resume execution.
classtv{floatPrice ; String name; TV (String name,floatPrice ) { This. price=Price ; This. name=name; }}classThreadjoinImplementsrunnable{TV TV; Thread Customer,tvmaker; Threadjoin () {Customer=NewThread ( This, "Customer"); Tvmaker=NewThread ( This, "TV maker"); } Public voidrun () {if(Thread.CurrentThread () = =customer) {System.out.println (Customer.getname ()+ "et" +tvmaker.getname () + "Production TV"); Try{tvmaker.join ();//thread customer starts waiting for Tvmaker to end}Catch(Interruptedexception e) {} System.out.println (Customer.getname ()+ "bought a TV" +tv.name+ "Price:" +Tv.price); } Else if(Thread.CurrentThread () = =Tvmaker) {System.out.println (Tvmaker.getname ()+ "Start production TV ..."); Try{tvmaker.sleep (2000); }Catch(Interruptedexception e) {} TV=NewTV ("Changhong card", 4500); System.out.println (Tvmaker.getname ()+ "Production finished!!! "); } }} Public classjointhread{ Public Static voidMain (String args[]) {threadjoin th=NewThreadjoin (); Th.customer.start (); Th.tvMaker.start (); }}
Java Line Program Control system