Public classmultithread { Public Static voidMain (String args[]) {System.out.println ("I'm the main thread!"); //The following creates a thread instance Thread1Threaduseextends thread1=Newthreaduseextends ();//Thread2 is created to implement the Thhreaduserunnable class instance of the Runnable interface as a parameterThread thread2=NewThread (NewThreaduserunnable (), "Secondthread"); Thread1.start ();//start thread thread1 to make it in a ready state//thread1.setpriority (6);//set the THREAD1 priority to 6//The priority will determine when the CPU is vacated, the thread in the ready state who first occupies the CPU starts to run//priority range 1 to 10,min_priority,max_priority,norm_paiority//new thread inherits create her parent thread priority, parent thread usually has normal priority i.e. 5norm_prioritySYSTEM.OUT.PRINTLN ("The main thread will hang for 7 seconds!")); Try{Thread.Sleep (7000);//main thread hangs 7 seconds} Catch(interruptedexception e) {return; } System.out.println ("Back to the main thread!"); if(Thread1.isalive ()) {thread1.stop ();//if Thread1 still exists, kill him .System.out.println ("Thread1 sleep too long, the main thread killed the thread1!"); } ElseSystem.out.println ("The main thread did not find that the thread1,thread1 was awake in order to complete the execution!"); Thread2.start ();//Start Thread2SYSTEM.OUT.PRINTLN ("The main thread will hang again for 7 seconds!")); Try{Thread.Sleep (7000);//main thread hangs 7 seconds} Catch(interruptedexception e) {return; } System.out.println ("Back to the main thread!"); if(Thread2.isalive ()) {thread2.stop ();//if Thread2 still exists, kill him .System.out.println ("Thread2 sleep too long, the main thread killed the thread2!"); } ElseSystem.out.println ("The main thread did not find that the thread2,thread2 was awake in order to complete the execution!"); System.out.println ("Program End Press any key to continue!"); Try{System.in.read ();}Catch(IOException e) {System.out.println (e.tostring ());}}//Main}//multithreadclassThreaduseextendsextendsThread//by inheriting the thread class and implementing its abstract method run ()//Create an instance of this thread subclass as appropriate to implement the multithreading mechanism//once a thread is started (that is, it is ready), the CPU will automatically call its run () method{threaduseextends () {}//constructor Function Public voidrun () {System.out.println ("I'm a thread instance of threads sub-Class!"); System.out.println ("I'll hang up for 10 seconds!"); System.out.println ("Back to the main thread, please wait a moment, the main thread hangs may not wake up!" "); Try{sleep (10000);//Suspend 5 Seconds} Catch(interruptedexception e) {return; } //if the run () method is executed, the thread will end automatically and not be killed by the main path//However, if the sleep time is too long, the thread is still alive and may be killed by Stop ()} }classThreaduserunnableImplementsRunnable//by implementing the Run () method in the Runnable interface, the class of the run () method is implemented with this//to create thread instances of thread for parameters{ //thread Thread2=new thread (this);//creates a thread instance of the thread class for the parameter in this class that implements the run () method in the Runnable interfaceThreaduserunnable () {}//constructor Function Public voidrun () {System.out.println ("I am a thread instance of the threads class and have implemented the Runnable interface class as Parameters!"); System.out.println ("I'll hang up for 1 seconds!"); System.out.println ("Back to the main thread, please wait for jn0-120 e20-040, just hang up the main thread may not wake up!" "); Try{Thread.Sleep (1000);//Suspend 5 Seconds} Catch(interruptedexception e) {return; } //if the run () method is executed, the thread will end automatically and not be killed by the main path//However, if the sleep time is too long, the thread is still alive and may be killed by Stop ()}} //The program can make changes such as changing the sleep time or priority Setpriorit
Java Multi-Threading Understanding Dark Horse Programmer