Learning Note Summary of methods of thread class in 4:java

Source: Internet
Author: User

A brief summary of the common methods in the thread class was made, and the impression was deepened.

Still the case of inheriting the thread class is used to implement the way the thread was created, with the code snippet:

/* *  @auto:  The common methods of  * 2015-10-16 * thread, a shepherd's queen, a little summary: * 1.  Start ()   Start the thread and execute the corresponding run () method  * 2. run ()    the code to be executed by the child thread into the run () method  * 3.  CurrentThread ()   static,  The current thread, which returns a reference to the currently executing thread object.  * 4. getname ():  Gets the name of this thread  * 5. setname ():  sets the name of this thread  * 6.  yield ()   :  pauses the currently executing thread object and executes other threads. That is, the thread that calls this method frees the current CPU for use, and the concession is granted.  * 7. join ()  : Waits for the thread to terminate. That is, the join () method of the B-thread is called in the A-thread, indicating that when executed to this method, the a thread stops executing until the B thread finishes executing, and the  * a thread then executes the code after join ()  * 8. isalive (  :  determines whether the current thread is still alive  , Boolean, if the death State returns False * 9. sleep (LONG&NBSP;M)   shows the current thread to sleep  m  milliseconds   Remember: The overridden method cannot throw a larger exception than the parent class.  * 10.  three methods in thread communication:  wait ()   notify ()    notifyall ()   *   * 11.  Set thread Priority: High priority thread preemption cpu  *         First Getproority ():  return threadPriority value  *        setproority (int newproority)  :  Change the priority of a thread  * */  class subthread extends thread {@Overridepublic   Void run ()  {for ( int i=0; i<=100; i++ ) {try {//  The overridden method cannot throw a larger exception   than the parent class.   So can only Try-catchthread.currentthread (). Sleep ( 1000 );}  catch  (interruptedexception e)  {e.printstacktrace ();} System.out.println ( thread.currentthread (). GetName ()  + ": "  + thread.currentthread (). GetPriority ()  + ": "  + i );}} Class testthread{public static void main (String[] args)  {  // The implied main thread Thread.CurrentThread (). SetName ("==========main main thread"); //  the thread name to the main thread subthread st =  new subthread (); St.setname ("   sub-thread");  //  set the name of the child thread st.setpriority (  thread.max_priority );  //  Set ChildThe thread is the highest priority  ,  10    defaults to 5   minimum of  1 st.start (); for ( int i =0; i<=100; i++ ) {System.out.println ( thread.currentthread (). GetName ()  + ": "  + thread.currentthread (). GetPriority ()  + ": "  + i )//if ( i %  10 == 0 ) {//system.out.println ("Pause main thread, execute sub-thread:");//thread.currentthread (). yield ();//}/*if (  i == 30 ) {System.out.println ("start to pause the main thread and execute a sub-thread:"); Try {st.join (); System.out.println ("Wait for the child thread to complete before executing the main thread:");}  catch  (interruptedexception e)  {e.printstacktrace ();}} */}SYSTEM.OUT.PRINTLN ("See if the child thread St still survives:  "  + st.isalive ()  );}}




Learning Note Summary of methods of thread class in 4:java

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.