JAVA synchronization code block and synchronization function __linux

Source: Internet
Author: User
Execution eligibility: Open thread but not executed, waiting for execution to be called qualified for execution. But there is no executive power.
Execution right: The thread being executed is called execution. (with Executive qualification)
Thread safety issues:
When more than one thread is open, a thread may appear that the first statement that just executes a function is jumped, and the next thread starts executing, such as if the first thread just executes the IF statement without executing the statement after the IF, and the second thread starts executing. When the first thread is executed again, the data may have changed. Which leads to security issues.

Synchronization functions and synchronized code blocks are all designed to address thread-safety issues.


Synchronizing code blocks
Thread-safety issues are resolved by synchronizing code blocks. By synchronizing code blocks, when a thread enters a synchronized code block, the thread can enter the synchronized code block only if it finishes executing all of the contents of the synchronization block, during which the thread in the synchronized code block loses execution and other threads that have execution rights cannot enter the synchronized code block.
Resolves thread-safety issues
But reduced efficiency, when a thread in the synchronization code block without the execution of the power of another thread can not perform the function, can only wait to jump the transfer of the code block in the synchronization of the thread to execute, the middle of the waiting time is wasted efficiency.
Synchronization (object) objects are arbitrary as long as it is guaranteed that multiple threads use the same object.


Synchronization functions
A synchronization function is the code that is to be executed encapsulated into a function.

The object behind the synchronization function is this and cannot be changed.


The more you use is the synchronized code block, because the object of the synchronized code block can be changed. A synchronous function can be said to be a shorthand form of a synchronized code block. It fixed the object as this.

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.