Import java.io. *; // Multi-thread programming public class multithread {public static void main (string args []) {system.out.println ("I am main thread!"); // Create a thread instance thread1threaduseExtends thread1 = new ThreadUseExtends (); // create thread2 time to achieve the THhreadUseRunnable class instance Runnable interface parameters thread thread2 = new thread (new ThreadUseRunnable (), "SecondThread"); thread1.start (); // start the thread thread1 In the ready state //thread1.setPriority (6 ); // Setting Thread1 priority to 6 // The priority will determine the CPU empty out, the thread is in the ready state, who first occupied the CPU to start running // Priority range 1 To 10, Min_Priority, Max_Priority, Norm_Paiority // New thread inheritage creates her parent thread priority, the parent thread usually has ordinary priority 5norm_prioritysystem.out.println ("The main thread will hang 7 seconds!"); Try {thread .sleep (7000); // Main thread hangs 7 second} catch (interruptedException e) {return;} system.out.println ("returned to the main thread!"); if (thread1.isalive ()) {Thread1 .stop (); // If thread1 still kills him system.out.println ("Thread1 is too long, the main thread kills Thread1!");} elsesystem.Println ("Main thread did not find thread1 Thread1 has been completed in the order! "); thread2.start (); // Start thread2system.out.println (" Main thread will hang 7 seconds! "); try {thread.sleep (7000); / / Main thread hangs 7 second} catCh;} system.out.println ("returned to the main thread!"); If (thread2.isalive ()) {Thread 2.Stop (); // If thread2 still exists, he kills him system.out.println ("Thread2 sleep too long, the main thread killed Thread2!");} ElseSystem.Println ("The main thread did not find Thread2, thread2 has been embedded in the order! "); system.out.println (" Program end pressing any button! "); try {system.in.read ();} catch (ooexception e) {system.out .println (e.tostring ());}} // main} // multithreadclass threaduseextends Extends thread // Create this THREAD sub-class by inheriting the Thread class, and implementing its abstract method Run () // To implement multi-thread mechanism // After a thread is started (i.e. Println ("I am Thread Subclass Thread Example!"); System.out.Println ("I will hang 10 seconds!"); system.out.println ("Back to the main thread, please wait, just the Lord Thread hang may not wake up! "); try {Sleep (10000);
// Hang a 5 second} catCh;} // If the run () method is executed, the thread will end, without killing // by the main thread, but if the sleep time is too long, The thread is also survived, which may be killed by STOP ()}} Class ThreadusenNable Implements runnable // Create thread thread by implementing the Run () method in the Runnable interface. Example {// thread thread2 = new thread (this); // Create thread instance for ThreadUnserunnable () {} // with this implementation of the Run () method in the runnable interface as a parameter threaduserunnable () {} // Constructor PUBLIC VOID RUN () {System.out.Println ("I am the thread instance of the Thread class and the class of the runnable interface is parameter!"); System.out.println ("I will hang 1 second!"); System.out. Println ("Back to the main thread, please wait, just the main thread hangs may not wake up!"); try {thread.sleep (1000); // Hang 5 second} catch (interruptedException E) {Return; } // If the Run () method is executed, the thread will end automatically without killing // by the main thread, but if the sleep time is too long, the thread is still survive, it may be killed by STOP ()}} / / This program can make modifications such as rehabilitation time or priority setPriority ()