Java multi-thread understanding, dark horse programmer, java Dark Horse

Source: Internet
Author: User

Java multi-thread understanding, dark horse programmer, java Dark Horse

Public class MultiThread {public static void main (String args []) {System. out. println ("I am the main thread! "); // Create a thread instance thread1 ThreadUseExtends thread1 = new ThreadUseExtends () below (); // when thread2 is created, Thread thread2 = new Thread (new ThreadUseRunnable (), "SecondThread"); thread1.start (); // start the thread thread1 to make it ready // thread1.setPriority (6); // set the priority of thread1 to 6 // The priority will determine when the cpu is idle, the ready thread who occupies the cpu first starts to run // priority range: 1 to 10, MIN_PRIORITY, MAX_PRIORITY, norm_priority ority // The New thread inherits the priority of the parent thread created for her, the parent thread usually has a common priority of 5NORM_PRIORI. TY System. out. println ("the main thread will be suspended for 7 seconds! "); Try {Thread. sleep (7000); // The main thread suspends for 7 seconds} catch (InterruptedException e) {return;} System. out. println ("back to the main thread! "); If (thread1.isAlive () {thread1.stop (); // if thread1 still exists, kill System. out. println (" thread1 sleep too long, and the main thread kills thread1! ");} Else System. out. println (" thread1 is not found in the main thread. thread1 is awake and the execution is finished! "); Thread2.start (); // start thread2 System. out. println (" the main thread will be suspended for another 7 seconds! "); Try {Thread. sleep (7000); // The main thread suspends for 7 seconds} catch (InterruptedException e) {return;} System. out. println ("back to the main thread! "); If (thread2.isAlive () {thread2.stop (); // if thread2 still exists, kill System. out. println (" thread2 sleep too long, and the main thread kills thread2! ");} Else System. out. println (" thread2 is not found in the main thread. thread2 is awake and the execution is finished! "); System. out. println (" press any key to continue after the program ends! "); Try {System. in. read ();} catch (IOException e) {System. out. println (e. toString () ;}// main} // MultiThreadclass ThreadUseExtends extends Thread // inherits the Thread class and implements 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 enters the ready state), its run () will be automatically called once the CPU is obtained () method {ThreadUseExtends () {}// constructor public void run () {System. out. println ("I Am a Thread sub-class Thread instance! "); System. out. println (" I will suspend for 10 seconds! "); System. out. println (" return to the main thread. Please wait. The main thread has been suspended! "); Try {sleep (10000); // suspend for 5 seconds} catch (InterruptedException e) {return ;}// if the run () method is executed in sequence, the thread will automatically end without being killed by the main thread. // However, if the sleep duration is too long, the thread will survive and may be stopped () kill} class ThreadUseRunnable implements Runnable // implement the run () method in the Runnable interface, and then implement the run () method class // The Thread instance for creating the Thread for the parameter {// Thread thread2 = new Thread (this); // run () in the Runnable interface () threadUseRunnable () {}// constructor public void run () {System. out. println ("I Am a Thread instance and take the class that implements the Runnable interface as the parameter! "); System. out. println (" I will suspend for 1 second! "); System. out. println (" back to the main thread, please wait for the jn0-120 e20-040, just the main thread suspended may not wake up! "); Try {Thread. sleep (1000); // suspend for 5 seconds} catch (InterruptedException e) {return;} // If the run () method is executed in sequence, the thread ends automatically, instead of being killed by the main thread. // However, if the sleep duration is too long, the thread remains alive and may be stopped () kill} // modifications to be made to the program, such as modifying the sleep time or priority setPriorit

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.