2016/9/25 some insights into synchronized (synchronous code block) When compiling Java Experiment Report

Source: Internet
Author: User
Tags thread class

Through this experiment, we understand the multi-threaded setup and startup, the use of synchronized code blocks, the priority usage of threads, and the knowledge that the classes of resources are thread-shared.

I now understand that multithreading is: instantiating a class that inherits the thread class or implements the Runnable interface (inheritance is to make it eligible to participate in multithreading), and then the code in the class run () is executed by the thread class to enable multi-threaded synchronous operation

After browsing through the web blog, and code to try to further understand the synchronization code block:

The ①synchronized () {} code block is executed to determine whether the object in parentheses is locked:

If none, then lock and start executing code in the code block

If so, enter the blocking queue and wait for the wake-up

When ②synchronized modifies a non-static function, the locked object is this, which is the object that currently references this function.

When ③synchronized modifies a static function, because the static/static method belongs to the class, the current class is locked, and when the static function is accessed, all the threads that want to access the function enter the blocking queue

④synchronized is said to be able to decorate a class,

class ClassName {   publicvoid  method () {      synchronized(ClassName.  Class) {         //  todo      }    }}

However, the experiment seems to be not mutually exclusive access, should be related to generics, should be in the near future to understand the generic section

Reference from: http://blog.csdn.net/luoweifu/article/details/46613015

2016/9/25 some insights into synchronized (synchronous code block) When compiling Java Experiment Report

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.