Java multithreading--two ways to create threads

Source: Internet
Author: User

Create thread mode One: Inherit the Thread class.

Steps:
1, define a class that inherits the thread class.
2, overwrite the Run method in the thread class.
3, create a thread for the subclass object that directly creates the thread.
4, call the Start method to open the thread and invoke the thread's task to execute the Run method.

You can get the name of the thread through the thread's GetName thread-number (starting from 0)
The main thread's name is main.

Cases:

classDemoextendsthread{PrivateString name; Demo (String name) {Super(name);//Parent class constructor, name of the thread to be rerouted//this.name = name;    }    //The ***run method is defined as the task code that the thread will run. ***     Public voidrun () { for(intx=0; x<10; X + +)        {            //for (int y=-9999999; y<999999999; y++) {}System.out.println (name+ ".... x=" +x+ "... name=" +Thread.CurrentThread (). GetName ()); }    }}classThreadDemo2 { Public Static voidMain (string[] args) {Demo D1=NewDemo ("Wang Choi"); Demo D2=NewDemo ("Xiaoqiang"); D1.start ();//Open the thread and call the Run method. D2.start (); System.out.println ("Over ...." +Thread.CurrentThread (). GetName ()); }}

Create thread mode two : When the class has its own parent class, override the Run method by implementing the Runnable interface. (* common *)

Steps:

1, defines the class implementation runnable interface.
2, overwrite the Run method in the interface, encapsulating the thread's task code into the Run method.
3, the thread object is created through the thread class and the subclass object of the Runnable interface is passed as a parameter to the constructor of the thread class.
Why? Because the threads ' tasks are encapsulated in the Run method of the Runnable interface subclass object.
Therefore, when you create a thread object, you must explicitly define the task that you want to run.
4, call the Thread object's Start method to open the thread.

Benefits of implementing the Runnable interface:

1, the task of the thread is separated from the subclass of the thread and carried out in a separate package.
Encapsulates a task into an object based on object-oriented thinking.
2, avoids the limitations of Java single inheritance.

Cases:

classDemoImplementsRunnable//extends Fu//prepare to extend the functionality of the demo class so that its contents can be executed as a thread's task. //completed via an interface. {     Public voidrun () {show (); }     Public voidShow () { for(intx=0; x<20; X + +) {System.out.println (Thread.CurrentThread (). GetName ()+"....."+x); }    }}classthreaddemo{ Public Static voidMain (string[] args) {Demo d=NewDemo (); Thread T1=NewThread (d); Thread T2=NewThread (d);        T1.start ();    T2.start (); }}

Java multithreading--two ways to create threads

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.