Java Multithreaded Development Series two: How to create multithreading

Source: Internet
Author: User

The previous article has introduced the basic knowledge of multithreading, such as what is multi-threading, what is the process, why use multithreading and so on.

After understanding the basics of using multithreading in software development, let's talk today about how to use multithreading in a simple way.

There are two ways to create multithreading in Java:

(1) Write a subclass that inherits from the thread class, at which time the subclass must override the Run method in the thread class (which I'll mention later in this article), and then we can use this class to create a multithreading.

(2) Still write a class, this class to implement the Runnable interface, and (1) the same, in this implementation class also need to rewrite the Run method.

one thing to note here is that C # is free to use any method as a method of a new thread, and Java has to implement this class in either of the two ways, and then 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 rewrite the Run () method, run (), the Run method, which is the specific task or function that the new thread will run in the future.

2. Instantiate (new) out the subclass that you just defined

3. Run the Start method for this new object. It is important to remember that the Start method is the only way to start a new thread. If you are running the Run method, it is still a simple single-threaded execution

1  Public classFirstthreadextendsThread2 {3     Private inti;4      Public voidRun ()5     {6          for(; i<=100;i++)7         {8 System.out.println (GetName ());9         }Ten     } One      Public Static voidMain (string[] args) A     { -          for(inti=0;i<100;i++) -         { the             if(i%10==0) -             { -                 NewFrirstthread.start (); -                 NewFrirstthread.start (); +             } -         } +     }  A}

Use the second method: to open a new thread by implementing the Runnable interface

1, define a class, this class needs to implement the Runnable interface, still need to rewrite the interface in this class the Run method, as in Method 1, the Run method is also the future thread execution body

2. Instantiate (new) out the class A just defined

3. Instantiate (new) a thread class and run the Start method with a as Tartget

1  Public classSecondthreadImplementsRunnable2 {3     Private inti;4      Public voidRun ()5     {6          for(; i<=100;i++)7         {8 System.out.println (Thread.CurrentThread (). GetName ());9         }Ten     } One      Public Static voidMain (string[] args) A     { -          for(inti=0;i<100;i++) -         { the             if(i%10==0) -             { -Secondthread st=NewSecondthread () -                 NewThread.Start (St, "name1"); +                 NewThread.Start (St, "name2"); -             } +         } A     }  at}

Comparison of two ways to open up multithreading

1. In terms of inheritance: Since Java does not allow multiple inheritance, it is significantly more cumbersome to approach 1 (using the thread subclass) than method two (because method 2 can inherit a parent class) if there is a need to inherit from a base class.

2, sharing data aspect: Method 2 can directly take the newly defined class as the target of each thread object (thread instance), so that the target object of each thread object can be said to be shared, we can work together the same resource (put in target), And the method requires another method to write, or to modify the constructor

3. Method 1 is much simpler and easier to understand than Method 2 (I feel that way). In the Run method, if you need to use a method of the current thread object (such as Getname,setname) or a field, method one can be used directly (because it is itself the thread object being executed), and method two needs to use Thread.CurrentThread () method to get the currently executing thread object (because this method is being run, so when the thread object is the threading object that runs the method (a bit of a mouthful), a little bit around the flavor), it then invokes the method of the current thread object.

4, regardless of which method to open multi-threading, do not forget, in addition to opening and running a new thread, itself there is an executing thread (two examples are the main thread)

5, Either way, if you want to use a new thread to execute the method body, you need to use the Start method to run the run passively, but not directly run the Run method (this is a simple sequential structure, the main line routines wait until the Run method ends, To start running).

Java Multithreaded Development Series two: How to create multithreading

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.