2017 45th Monday Java Multithreading creation method

Source: Internet
Author: User

1. Starting with JDK1.5, Java provides 3 ways to create and start Multithreading:

Ø inherit the thread class to create the threading class, overriding the run () method as the thread execution body.

Ø implement the Runnable interface to create the thread class, overriding the run () method as the thread execution body.

Ø implement the callable interface to create the thread class, overriding the run () method as the thread execution body.

The first of these is the worst, and it has 2 disadvantages:

The thread class inherits the thread class and cannot inherit other parent classes.

Because each thread is an instance of a thread subclass, it can be cumbersome to share data among multiple threads.

For the 23rd way, they are essentially the same, except that the call () method contained in the callable interface can either declare a throw exception or have a return value.

2. In addition, the start thread should use the start () method instead of the run () method. If the program never invokes the start () method of the thread object to start it, then the thread object will remain in the new State (1. New 2. Ready 3. Run 4. Block 5. Death Total 5 states), it will never be an opportunity for the thread to execute, it is just an ordinary Java object. When a program calls the run () method of a thread object, there is no difference from the normal method of invoking a normal Java object, so it will never start a new thread.

3. Static Synchronization method:

Java language rules: Before any thread enters the synchronization method, the synchronization method must be obtained before synchronizing the code block, and the synchronization monitor corresponding to the code block is synchronized. For a synchronous code block, the program must display the synchronization monitor specified for it, and for a synchronous non-static method, the synchronization monitor of the method is the Java object that this-calls the method, and for a static synchronization method, the method's synchronization monitor is not this, but the class itself.

2017 45th Monday Java Multithreading creation method

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.