Java multi-thread Development Series II: how to create multi-thread and multi-thread Series II

Source: Internet
Author: User

Java multi-thread Development Series II: how to create multi-thread and multi-thread Series II

I have already introduced the basic knowledge of multithreading, such as what is multithreading, what is a process, and why multithreading is used.

After learning the basic knowledge about multithreading in software development, let's talk about how to use multithreading.

There are two ways to create multithreading in Java:

(1) Write a subclass that inherits from the Thread class. At the same time, this subclass must overwrite the run method in the Thread class (as mentioned later ), then we can use this class to create a multi-thread.

(2) Write a class. This class must implement the Runnable interface, which is the same as (1). In this implementation class, you also need to override the run method.

Note that: C # Any method can be used as a new thread method at will. java must implement this class using any of the above two methods, use this class to create a new thread.

Use the first method: Inherit the Thread parent class

1. Define a subclass that inherits from the Thread class and override the run () method and run (). This run method is the specific task or function to be run by the new Thread in the future.

2. instantiate (new) The subclass just defined

3. Run the start method of the new object. Remember to use the start method to start a new thread. If you are running the run method, it is still a simple single-thread execution.

 1 public class FirstThread extends Thread 2 { 3     private int i; 4     public void run() 5     { 6         for(;i<=100;i++) 7         { 8             System.out.println(getName()); 9         }10     }11     public static void main(String[] args)12     {13         for(int i=0;i<100;i++)14         {15             if(i%10==0)16             {17                 new FrirstThread.start();18                 new FrirstThread.start();19             }20         }21     } 22 }

Method 2: implement the Runnable interface to open up new threads

1. Define a class. This class needs to implement the Runnable interface, and you still need to re-write the run method in the interface in this class. Like method 1, this run method is also the future thread execution body.

2. instantiate (new) the Class A just defined

3. instantiate (new) A Thread class and use A as the tartget to run the start method.

 1 public class SecondThread implements Runnable 2 { 3     private int i; 4     public void run() 5     { 6         for(;i<=100;i++) 7         { 8             System.out.println(Thread.currentThread().getName()); 9         }10     }11     public static void main(String[] args)12     {13         for(int i=0;i<100;i++)14         {15             if(i%10==0)16             {17                 SecondThread st=new SecondThread()18                 new Thread.start(st,"name1");19                 new Thread.start(st,"name2");20             }21         }22     } 23 }

 

Comparison of the two methods for developing Multithreading

1. Inheritance: Because java does not allow multiple inheritance, if a base class needs to be inherited, method 1 (using the Thread subclass) the method is obviously more troublesome than method 2 (implementing the Runnable interface) (because method 2 can inherit another parent class)

2. Data Sharing: method 2 can directly use the newly defined class as the target of each Thread object (Thread instance), so that the target object of each Thread object can be shared, you can process the same resource together (put it in target), while method 1 requires writing another method, or modifying the constructor.

3. method 1 is much simpler and easier to understand than method 2 (I think so ). In the run method, if you need to use the method (such as getName, setName) or field of the current thread object, you can directly use method 1 (because it is the thread object being executed ), the second method requires Thread. currentThread () method to obtain the currently executed thread object (because this method is being run, the current thread object is the thread object that runs this method (a bit of Interface ), and then call the method of the current thread object.

4. No matter which method is used to open up multiple threads, do not forget that in addition to opening up and running new threads, there is also a thread in progress (in both examples, it is a Main thread)

5. Regardless of the method, if you want to use the new thread to execute the method body, you must useStart MethodInstead of directly running the run method (this is a simple sequence structure, the main thread will continue to run after the run method is complete ).

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.