Java multi-threaded learning initial classic example

xiaoxiao2021-03-06  76

Java multi-threaded learning initial classic example

Sender: RealWHS (AMO TE) Conservation: Abhandsome (2004-03-23 ​​10:37:57),

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 IXTENDS 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 ("Main thread Didn't find Thread1, thread1 has been completed in the order! "); Thread2.start (); // Start Thread2 System.out.println (" The main thread will hang 7 seconds! "); Try {thread.sleep 7000); // Main thread hangs 7 second} catCh (InterruptedException E) {return;} system.out.printl n ("returned to the main thread!"); if (thread2.isalive ()) {thread2.stop (); // If thread2 still kills him system.out.println ("Thread2 is too long, the main Thread 2! ");} 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 to continue! " ); try {system.in.read ();} catch (ooException e) {system.out.println (e.tostring ());}} // main} // multithread class threaduseExtends Extends thread // By inherited Thread Class, and implementing its abstract method Run () // When you create an instance of this THREAD subclass, you can implement multi-thread mechanism // After a thread is started (i.e., 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 Thread subclasses!"); System.out.println ("I will hang 10 second!");

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

New Post(0)