Multi-threaded Learning notes (i)

Source: Internet
Author: User

 PackageCom.thread;/*** Create a sub-thread output from the natural number of 1~100 * The first way to create multithreading, inherit the thread class * GetName get the name of the current thread * SetName set the name of the current thread * Start a child thread * yield the current thread releases the CPU resources, If no other thread is occupied then the thread will continue to execute, and if there are other threads then it may be preempted by another thread * Join calls the method of the B thread in the A thread, indicating: When the A method executes to the method, the B method is executed, and the B method is executed before execution continues * IsAlive to determine if the current thread is still alive * sleep (Long L): Show the current thread sleep L milliseconds * thread communication, wait notify Notifyall * * Set Thread Priority * GetPriority () get priority of current thread * s Etpriority () Sets the priority of the current thread, sets the priority of the thread, does not allow the thread to execute after it finishes executing another thread, and only gives the thread the chance to grab the CPU resource, with a general default priority of 5 * *@authorAdministrator **/ Public classTestThread1 { Public Static voidMain (string[] args)throwsinterruptedexception {//Thread.CurrentThread (). SetName ("main thread");//SubThread1 subThread1 = new SubThread1 ();//Subthread1.setname ("sub-thread");//subthread1.setpriority (thread.max_priority);//set child threads to the highest priority: Ten//Subthread1.start ();//for (int i = 1; i <=; i++) {//System.out.println (Thread.CurrentThread (). GetName () + ":" +thread.currentthread (). GetPriority () + ":" +i);//if (i = =) {//Subthread1.yield ();//frees CPU resources when the main thread executes to 50//            }//if (i = =) {//try {//Subthread1.join ();//when the main thread executes to 20, let the child thread join in.//} catch (Interruptedexception e) {//                    //TODO auto-generated Catch block//e.printstacktrace ();//                }//            }//        }                                //call different threads to print even and oddOddtest odd =Newoddtest (); Odd.setname ("Print Odd");        Odd.start (); Eventest even=Neweventest (); Even.setname ("Print even");    Even.start (); }    }classSubThread1extendsthread{//overriding the Run method@Override Public voidrun () { for(inti = 1; I <= 100; i++){//try {//Thread.Sleep (+);//1 seconds per sleep//} catch (Interruptedexception e) {//                //TODO auto-generated Catch block//e.printstacktrace ();//            }System.out.println (Thread.CurrentThread (). GetName () + ":" +thread.currentthread (). GetPriority () + ":" +i); }    }}        /*** Print Odd number *@authorAdministrator **/    classOddtestextendsthread{@Override Public voidrun () { for(inti = 0; I < 100; i++){                if(i% 2! = 0) {System.out.println (Thread.CurrentThread (). GetName ()+":"+i); }            }        }            }        /*** Print even number *@authorAdministrator **/    classEventestextendsthread{@Override Public voidrun () { for(inti = 0; I < 100; i++){                if(i% 2 = = 0) {System.out.println (Thread.CurrentThread (). GetName ()+":"+i); }            }        }            }

Multi-threaded Learning notes (i)

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.