[Javase] Multithreading (Join method)

Source: Internet
Author: User

Multiple threads execute concurrently, randomly switching, calling the join () method, which freezes the thread on which the current thread is located (the general main thread) until the current thread ends and the thread resumes execution

classJointestdemoImplementsrunnable{@Override Public voidrun () { for(intx=0;x<=5;x++){            Try{Thread.Sleep (100); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+"===="+x); }    }    } Public classJoindemo {/**     * @paramargs *@throwsinterruptedexception*/     Public Static voidMain (string[] args)throwsinterruptedexception {Jointestdemo Join=NewJointestdemo (); Thread T1=NewThread (join); Thread T2=NewThread (join);        T1.start ();        T2.start (); //the above two sub-threads are executed alternately, the main thread freezes, and the T1 is thawed out.T1.join (); //Show main thread         for(intx=0;x<=5;x++) {Thread.Sleep (100); System.out.println (Thread.CurrentThread (). GetName ()+"===="+x); }    }}

Thread priority, call the setpriority () method of the thread object to set the priority, parameters:1,5 , the most obvious;thread.max_priority,thread.min_priority ,thread.norm_priority

Call Thread.yield (); can temporarily release execution, to achieve the purpose of the average running of the thread

[Javase] Multithreading (Join method)

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.