[Recommended Example] A typical multi-threaded program

xiaoxiao2021-03-06  38

Import java.io. *; // Multi-threaded programming public class multithread {public static void main (string args []) {system.out.println ("I am main thread!"); // Create a thread instance thread1 threaduseextends 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 Make it in the ready state //thread1.setPriority (6 );// Setting Thread1 priority to 6 // The priority will determine the CPU empty out, who 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 inherit creates her parent thread priority, the parent thread usually has ordinary priority 5norm_priority system.out.println ("The main thread will hang for 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 exists, he kills him system.out.println ("Thread1 is too long, the main thread killed Thread1!"); Else System.Out.println ("The main thread did not discover Thread1, thread1 has been completed in the order!"); thread2.start (); // Start Thread2 System.out.println ("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 (thread2 .isalive ()) {thread2.stop (); // If thread2 also exists, he kills him system.out.println ("Thread2 is too long, the main thread kills thread2!");} else system.out. Println ("The main thread did not discover Thread2, thread2 has been completed in the order!"); system.out.println ("Program end pressing any key!")

Try {system.in.read ();} catch (ooexception e) {system.out.println (e.tostring ());}} // main} // MultithRead

Class ThreaduseExtends Extends Thread // Create a multi-thread mechanism // one thread is started after the instance of this THREAD subclass is created by inheriting the Thread class Run () // appropriate time (also entering ready state) Once the CPU will automatically call its Run () method {

ThreaduseExtends ()} // Constructor PUBLIC VOID RUN () {System.out.println ("I am thread instance of the Thread subclass!"); System.out.println ("I will hang 10 seconds!") System.out.println ("Back to the main thread, please wait, just the main thread hangs may not wake up!"); Try {sleep (10000); // 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 () }

Class Threaduserunnable IMPLEMENTS RunNable / / Create thread's thread instances for THREAD by implementing the Run () method in the Runnable interface, creating threads of thread (this); this); // thread thread2 = new thread (this); / / With this class of Run () methods in the runnable interface, the thread of the threadUns // constructor () {} // constructor () {system.out.println ("I am thread with the Thread class" Examples and to implement the class of the Runnable interface as parameters! "); System.out.println (" I will hang 1 second! "); System.out.println (" Back to the main thread, please wait, just the Lord Thread hang may not wake up! "); Try {thread.sleep (1000); // Hang 5 second} catch (interruptedException e) {return;} // If the run () method is executed, thread It will be automatically ended 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 ()}

} // The program can make changes such as change sleep time or priority setPriority ()

转载请注明原文地址:https://www.9cbs.com/read-65014.html

New Post(0)