Two methods for creating multithreading in Java (inheriting the Thread class/implementing the runnable interface)

Source: Internet
Author: User

Thread creation steps:

The steps for creating a thread are as follows. (1) Create a New Thread class that inherits the Thread class and overwrites the run () method of the thread class. Class threadtype extends thread {public void run (){......} (2) create a Thread class object. The creation method is the same as that of a general object. The key word "new" is used to complete the creation. Threadtype TT = new threadtype (); (3) start the new thread object and call the START () method. TT. Start (); (4) the thread calls the run () method. Void run (); the following example shows how to create multiple threads. Class threaddemo2 extends thread {// declare No parameter. The empty constructor threaddemo2 () {}// declare the constructor threaddemo2 (string szname) {super (szname ); // call the constructor of the parent class} // overload the run function public void run () {for (int count = 1, row = 1; row <10; row ++, count ++) // Number of * output by cyclic computing {for (INT I = 0; I <count; I ++) // Number of the specified count in cyclic output * {system. out. print ('*'); // output *} system. out. println (); // output *} public static void main (string argv []) {threaddemo2 TD1 = new threaddemo2 (); // create, and initialize threaddemo2 type object TD1 threaddemo2 td2 = new threaddemo2 (); // create and initialize threaddemo2 type object td2 threaddemo2 td3 = new threaddemo2 (); // create, and initialize the threaddemo2 object td3 td1.start (); // start thread TD1 td2.start (); // start thread td2 td3.start (); // start thread td3 }}


Steps for creating a runnable thread:

Follow these steps to implement the runnable thread. (1) create a class that implements the runnable interface and rewrite the run method in this class. Class threadtype implements runnable {public void run (){......} (2) use the keyword new to create a threadtype instance. Runnable RB = new threadtype (); (3) create a thread object through the runnable instance. When creating a thread object, the called constructor is new thread (threadtype ), it uses the run () method implemented in threadtype as the run () method of the new thread object. Thread TD = new thread (RB); (4) Start the thread by calling the START () method of the threadtype object. TD. Start (); The following is an example of creating multithreading through runnable. // File: Program 10.5threaddemo4.java Description: generate three new Thread class threaddemo4 implements runnable {// overload the run function public void run () {for (int count = 1, row = 1; row <10; row ++, Count ++) // Number of X output by cyclic computing {for (INT I = 0; I <count; I ++) // Number of the specified count in cyclic output * {system. out. print ('*'); // output *} system. out. println (); // output line break} public static void main (string argv []) {runnable Rb1 = new threaddemo4 (); // create, and initialize threaddemo4 object rbrunnable Rb2 = new threaddemo4 (); // create and initialize threaddemo4 object Rb2 runnable Rb3 = new threaddemo4 (); // create, and initialize threaddemo4 object Rb3 thread TD1 = new thread (rb1.); // create a thread object TD1 thread td2 = new thread (Rb2 ); // create the thread object td2 thread td3 = new thread (Rb3); // create the thread object td3 td1.start (); // start the thread TD1 td2.start (); // start thread td2 td3.start (); // start thread td3 }}

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.