02_ Thread creation and startup _ Inherit the thread way

Source: Internet
Author: User
Tags instance method

Brief

Java uses the thread class to represent threads, and all threads must be instances of thread or its subclasses.

The task of each thread is to complete a certain task, in effect, to execute a program FLOW.

"to Create and start multi-threaded steps (integrated thread class mode)"

1. Define the subclass that inherits the thread class, overriding the run Method: the Run () method represents the task that the thread needs to complete, so the run () method is called the thread execution Body.

2. Create a thread object: creates an instance of the thread subclass.

3. Start thread: Call the start () method of the thread Object.

"sample Code firstthread.java"

package   COM. higgin.part01;//1.1 defines the subclass that inherits the thread class Firstthreadclass  firstthread  extends Thread{//1.2 Overriding the Run method     public void Run() { for(intI=0;i<Ten; i++){            //note: This is used directly when a thread inherits the thread class. GetName () to get the name of the current threadSystem. out. println ( this.GetName()+"===="+i); }    }} public classdemo1{ public Static voidmain (string[] Args) {//2. Create a Thread object         firstthread t1=New firstthread(); //3. Start the threadt1. Start        (); //2. Create a Thread object         firstthread T2=New firstthread(); //3. Start the threadt2. Start                ();  for(intI=0;i<Ten; i++){            //call Thread.currentthrad (). getName () to get the name of the current threadSystem. out. println (thread.currentthread (). getName ()+"===="+i); }    }}

"run results"

"note one: The main thread do not forget"

When the Java program starts running, the program will create at least one main thread, and don't forget that the method body of the main () method in this example is the thread execution body of the main threads.

"note Two: two ways to read a Thread's name"

1. Thread.CurrentThread (): CurrentThread () is a static method of the thread class that gets the thread object of the currently executing THREAD.

2.getName (): This method is an instance method of the thread class that returns the name of the thread that called the Method.

"note three: about the thread name"

1. By default, The main thread name is main, and the user-initiated threading name Is: Thread-0, Thread-1 ...

2. of course, You can set the name of the corresponding thread by SetName (String name).

02_ Thread creation and startup _ Inherit the thread way

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.