[Java concurrent programming practice] ----- basic thread concepts, java -----

Source: Internet
Author: User

[Java concurrent programming practice] ----- basic thread concepts, java -----

I have been learning Java concurrency for more than a month. I feel that I will forget some things after I have learned it for a while. I have made some notes but not the system. For a "system" with such a large Java concurrency ", you need to summarize and organize it to conquer it. I hope my colleagues can learn Java concurrent programming, make common progress, and provide mutual guidance.

Before learning Java concurrency, we need to understand some basic concepts: sharing, variable, thread security, thread synchronization, atomicity, visibility, and orderliness.

Shared and variable

To write thread-safe code, the core is to access shared and variable states.

"Sharing" means that variables can be accessed by multiple threads at the same time. We know that resources in the system are limited, and different threads have the same right to use resources. Limited and fair means competition, and competition may lead to thread problems.

Variable means that the value of a variable can change within its lifecycle. "Variable" corresponds to "immutable ". We know that immutable objects must be thread-safe and never require extra synchronization (because an immutable object only needs to be built correctly, its external visible State will never change ). So "variable" means there is a risk of thread insecurity. Solution:

1. If the status variable is not shared in the thread, the variable can be encapsulated in the method.

2. Change the state variable to an immutable variable (final ).

3. Use a synchronization policy when accessing status variables.

4. Use the atomic variable class.

Thread Security

Thread security is a complicated concept. Its core concept is correctness. The so-called correctness means that all kinds of behaviors are completely consistent with their specifications, that is, they are similar to what we know (we know it when we see it )". When multiple threads access a certain type of data, no matter what scheduling method the runtime environment adopts or how these threads will be executed alternately, and no extra synchronization or collaboration is required in the main code, this class can show correct behavior, so it is called thread security. (Reference: Java concurrent programming practices)

Thread Synchronization

The core of a thread is "Same ". The so-called "same" means collaboration, assistance, and cooperation, and "synchronization" means collaboration and pace, that is, running in a predetermined order, that is, "you first, I will wait, you have done it, I will do it again ".

Thread Synchronization means that when a thread sends a function call, the call will not return until the result is obtained, and other threads cannot call the method. In general, when we talk about synchronization and Asynchronization, we specifically refer to the tasks that require other components to work together or take some time to complete. In multi-thread programming, some sensitive data cannot be accessed by multiple threads at the same time. The thread synchronization technology is used to ensure that data can be accessed by only one thread at most at any time, ensure data integrity.

Thread synchronization mechanisms include: critical section, mutex, event, and semaphore.

1. critical section: accesses public resources or code segments through multi-thread serialization, which is fast and suitable for controlling data access. Only one thread is allowed to access Shared resources at any time. If multiple threads attempt to access public resources, after one thread enters, other threads trying to access public resources will be suspended until the threads entering the critical section exit. After the critical section is released, other threads can be preemptible.

2. mutex: Use the mutex object mechanism. Only threads with mutex objects have the permission to access public resources. Because there is only one mutex object, it can ensure that public resources are not simultaneously accessed by multiple threads. Mutual exclusion can not only achieve the security sharing of public resources for the same application, but also the security sharing of public resources for different applications.

3. semaphore: it allows multiple threads to access the same resource at the same time, but it must limit the maximum number of threads that can access the resource at the same time.

4. event: the notification operation is used to synchronize threads and compare the priority of multiple threads.

[Reference: Baidu encyclopedia]

Atomicity

An atom is the smallest unit in the world and is severable. In the world of programming, an operation is atomic if it is inseparable. For example: I = 0, this operation is inseparable, so this operation is atomic. If an operation can be split, the operation is not atomic, such as I ++. Non-atomic operations all have thread security issues. In this case, we need to use a synchronous mechanism to ensure that these operations become atomic operations to ensure thread security.

Visibility

Thread visibility refers to the visibility between threads, that is, the modification of a thread's status is visible to another thread, that is, the modification result of one thread, and the other thread will immediately know. For example, variables modified by volitile are visible.

Orderliness

Orderliness refers to the fact that, in the case of concurrency, the actual execution result is the same as the execution result of a single thread, and the result is not unpredictable due to the re-sorting problem. Volatile, final, synchronized, and explicit locks ensure order.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.