The simple application of multithreading

Source: Internet
Author: User

Well, when it comes to threading applications, we need to know the creation of threads first. Threads are created in two ways: first, inherit the thread class two, declare the implementation of the Runnable method

Method One:

 Packagethread;//inherit the thread class Public classPracticethreadextendsThread { Public intjudge;  PublicPracticethread (intjudge) {        Super();  This. Judge =judge; } @Override Public voidrun () {if(Judge < 0) {             for(Charch = ' a '; Ch < ' z '; ch++) {System.out.print (" " +ch); }        } Else {             for(Charch = ' A '; Ch < ' Z '; ch++) {System.out.print (" " +ch); }        }    }}

Method Two:

 Packagethread; Public classPracticethreadtImplementsRunnable { Public intjudge;  PublicPracticethreadt (intjudge) {        Super();  This. Judge =judge; } @Override Public voidrun () {if(Judge < 0) {             for(Charch = ' a '; Ch < ' z '; ch++) {System.out.print (" " +ch); }        } Else {             for(Charch = ' A '; Ch < ' Z '; ch++) {System.out.print (" " +ch); }        }    }}

There are two ways to start a thread:

 Packagethread; Public classTest {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubPracticethread pt =NewPracticethread (0) ; Practicethread pt2=NewPracticethread (-1) ; /*Thread implementation Method one*/Pt.start ();        Pt2.start (); /*Thread Implementation Method two*/         NewThread (PT). Start (); NewThread (PT2). Start ();  for(inti = 0; I < 5; i++) {Banzhuan<Object> BZ =NewBanzhuan<object> (5);        Bz.start (); }    }}

We know how to create and start a thread, so we need to know that the thread is also a priority. The lowest priority is: min_priority (1) The default priority is: Norm_priobity (5) The highest priority is: Max_priobity (10) Line Cheng think: thread-(0-max)-such as: Thread1, Thread2 .....

The simple application of multithreading

Related Article

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.