Multi-threaded traps

Source: Internet
Author: User
Tags thread class

1. There is a thread class object (implemented by inheriting the thread class), and if the program has never called the object's start () method to start it, then the thread object will always be in the "new" state, which is never an opportunity for the thread to execute, just a normal Java object, When calling its run () method directly, it is no different from ordinary methods of ordinary Java objects.

2. static method by adding synchronized keyword to achieve synchronization, at this time the method locks the object is the current class, That is Xxx.class, instead of a static method to achieve synchronization, you can directly add synchronized keyword to the method, or synchronized (this) {} to implement, at this time, the object is locked is an instance of the class object. Therefore, the static synchronization method and the synchronous code block with this as the synchronization monitor can be executed concurrently. The static synchronization method and the synchronous code block with the current class as the synchronization monitor cannot be executed concurrently.

3. Each class first executes the main thread to allocate memory and initialize.

Multi-threaded traps

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.