Java Multithreading Knowledge points Summary (four) multi-threaded knowledge point context diagram

Source: Internet
Author: User

1. Multithreading Security issues

1) Synchronized keyword: How to lock the problem, choose the Synchronized method or synchnized code block.

Select which lock question, this object, or class object (for static method or variable), or object

It is recommended to use a synchronous code block, because the lock is flexible and you can choose which lock to use, especially if there are multiple synchronization methods in a class.

Read and write problems, to ensure that multithreading to the same object read and write normal, not to read dirty data. Read and Write methods share the same lock.

Shared resources, which in turn involve a single-mode thread-safety problem, a threads pool, and so on.

2) thread-safe classes, such as stringbuffer,cocurrenthahmap,hashtable,vector, etc.

3) threadlocal, another kind of thinking to achieve multi-threaded security issues, commonly used in the database connection pool, the connnection into the threadlocal, to ensure that each thread uses the same connection, and thus ensure that the transaction is unified.

Synchronized is the principle of sequential access through multithreading, sacrificing efficiency.

Threadlocal is the way in which the variables are copied into each thread's working memory by expanding the memory, and each thread space has a separate copy of the variable.

Java Multithreading Knowledge points Summary (four) multi-threaded knowledge point context diagram

Related Article

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.