Better understanding of Java multithreading

Source: Internet
Author: User

1. Creation of threads

Prior to knowing that a multithreaded creation method in Java is to inherit the thread class or implement the Runable interface, but can not understand the following method of creating threads

First Kind

[Java]View PlainCopyprint?
  1. New Thread (new Runnable () {
  2. @Override
  3. public Void Run () {
  4. }
  5. }
  6. }). Start (); The new Thread () in this way creates a thread, and the new Runable () object is the code that the thread wants to execute, putting the code that you want to execute into a created object,
[Java]View PlainCopyprint?
    1. <span style="White-space:pre" > </span> //To well embody the object-oriented programming rules.


or the following creation format

The second Kind

[Java]View PlainCopy  print?
    1. new thread () {  
    2.          @Override   
    3.         public  void run ()  {  
    4.             // TODO  auto-generated method stub  
    5.              super.run ();   
    6.         }  
    7. &NBSP;&NBSP;&NBSP;&NBSP;}. Start ();   


In fact, two methods yesterday saw a video is now understood, in fact, the first method is the object-oriented thinking mode to create a thread, in the source code of thread, one is through the construction method to pass a Runable object and then assign a value to target, and then to implement a thread creation, That is, the first way to create a method, is to directly rewrite the Run method, which is the second method above to create a thread, but the Internet is usually the first method to use more? What's that for? Because the first way to create a thread by constructing a method is to have a more oriented rule, the new thread () creates an object, and the code that the object wants to execute is written in parentheses, which is a good representation of object-oriented programming.

So let's think about how the next way to create an object would be to execute the Run method in Runable or execute the thread's Run method?

[Java]View PlainCopyprint?
  1. New Thread (
  2. New Runnable () {
  3. public Void Run () {
  4. While (true) {
  5. try {
  6. Thread.Sleep (500);
  7. } catch (Interruptedexception e) {
  8. E.printstacktrace ();
  9. }
  10. System.out.println ("runable" + thread.currentthread (). GetName ());
  11. }
  12. }
  13. }
  14. ) {
  15. @Override
  16. public Void Run () {
  17. While (true) {
  18. try {
  19. Thread.Sleep (500);
  20. } catch (Interruptedexception e) {
  21. E.printstacktrace ();
  22. }
  23. System.out.println ("thread" + thread.currentthread (). GetName ());
  24. }
  25. }
  26. }.start ();


New Thread (New Runable () {}) {}.start (); The answer is to execute the Run method in Thread (that is, the Run method in the subclass). Since the run () method in the parent class is overridden when creating a new thread, the Run method in thread is executed directly, and the Run method in the parent class is executed if the Run method in the subclass does not exist.

Better understanding of Java 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.