A Java question every day [10]

Source: Internet
Author: User

Topic

Explains the two most common ways to create threads and their comparisons.

Answer

method One: Inherit the Thread class implementation

Steps:

    1. Create a subclass of the Thread class, such as MyThread.
    2. Override the run () method of the Thread class.
    3. Instantiate the MyThread class with object names such as MyThread.
    4. Use the start () method of the thread class to start a thread, such as Mythread.start ().

method Two: Implement Runnable interface

Steps:

    1. Create a class, such as Myrunnablethread, to implement the Runnable interface.
    2. Creates an object of the Myrunnablethread class.
    3. Instantiate the thread class, object name such as thread, and pass to its constructor the Myrunnablethread class and the thread name two parameters.
    4. Use the start () method of the thread class to start a thread, such as Thread.Start ().

Inheriting the thread class creating threads differs from implementing the Runnable interface creation thread in that when multiple threads are created with the same class, the former actually creates several different thread objects, and its internal The Run () method executes in its own object, does not interfere with each other, as many threads perform multiple tasks, while the latter actually uses the same object to create multiple threads, so the properties within the object are common, which is equivalent to multiple threads performing the same task.

In addition, the class inheriting the thread class cannot inherit other classes, and the class that implements the Runnable interface can inherit other classes, the former is relatively limited.

This may be a bit abstract, the following code to explain.

Reference Code

MyThread Class

 PackageMe.huangzijian; Public classMyThreadextendsThread {Private intnum = 10; PrivateString name;  PublicMyThread (String name) { This. Name =name; } @Override Public voidrun () {intCount =num;  for(inti = 0; I < count; i++) {System.out.println ( This. Name + ":" +num); Num--; }    }}

myrunnable Class

 PackageMe.huangzijian; Public classMyrunnablethreadImplementsRunnable {Private intnum = 10; @Override Public voidrun () {intCount =num;  for(inti = 0; I < count; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" +num); Num--; }    }}

Theadcreation class

 PackageMe.huangzijian; Public classthreadcreation { Public Static voidMain (string[] args) {//inheriting the thread class implementationMyThread MyThread1 =NewMyThread ("MyThread1"); MyThread myThread2=NewMyThread ("MyThread2"); MyThread myThread3=NewMyThread ("MyThread3");        Mythread1.start ();        Mythread2.start ();        Mythread3.start (); //implementing the Runnable interfaceMyrunnablethread Myrunnablethread =NewMyrunnablethread (); Thread T1=NewThread (Myrunnablethread, "MyRunnableThread1"); Thread T2=NewThread (Myrunnablethread, "MyRunnableThread2"); Thread T3=NewThread (Myrunnablethread, "MyRunnableThread3");        T1.start ();        T2.start ();    T3.start (); }}

Operation Result:

After running threadcreation , we will see that the result of inheriting the Thread class implementation is as follows:

As you can see, the three-thread operation for the field Num is each operation, reducing num from ten to 1.

The implementation of the Runnable interface is as follows:

As you can see, three threads work together on num for the same myrunnablethread object. Depending on the program, each thread loops ten times, so num has been reduced from ten to negative. Some friends may ask, why the first three threads will get to ten, this is the thread synchronization problem, need to use the synchronized and other keywords to decorate.

 

A Java question every day [10]

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.