Synchronized in Java

Source: Internet
Author: User
Translated from Java tutorial Thread Synchronization Synchronization)
Thread interference

Thread communication is mainly done by sharing the object domain and object references involved in the domain. This form of communication is extremely efficient. However, two errors may occur:Thread interferenceThread interference andMemory consistency errorsMemory consistency error. The tool to prevent these errors is synchronization.

Thread interfernce (Thread interference)

Consider a simple class:Counter

 
Class counter {private int C = 0; Public void increment () {C ++;} public void decrement () {c --;} public int value () {return C ;}}

counter for each class increment call C added 1 , and for each decrement call C reduces 1 . However, if a counter when an object is referenced by multiple threads, interference between threads may impede it from working as expected.

interference occurs when two operations running on different threads but acting on the same data are staggered. This means that the two operations contain multiple steps and the steps are in overlapping order. For counter the operation on the object is unlikely to leak, because the C two operations are a single simple statement. However, each simple statement is translated into multiple steps by the virtual machine. We do not check whether the virtual machine has been divided into several steps. It is obvious that Expression C ++ it can be divided into three steps:

1.Retrieve currentC.

2..RetrieveCValue increase1

3.After the storage is addedCValue

C --Break down in the same way

assume the thread A increment method, almost simultaneously thread B decrement method. If C the initial value is 0 the two threads may be staggered in the following sequence:

1Thread A: retrieve C.

2Thread B: retrieve C.

3Thread A: Increment retrieved value; result is 1.

4Thread B: decrement retrieved value; result is-1.

5Thread A: store result in C; C is now 1.

6Thread B: store result in C; C is now-1.

ThreadAThe result is lost because the threadBOverwrite. This crossover is only possible. In different environments, it may be a thread.BThe result is threadAOverwrite, or no error at all. Because errors are unpredictable, the errors for detecting and correcting thread interference are also different.


Memory consistency errors

When different threads access data, the data obtained by the original thread should be the same but different. Memory consistency errors are complex. We need a policy to avoid this problem.

The key to avoiding memory coherence errors is to understand a previous relationship. This relationship ensures that the memory written by a specified statement is visible to another statement. To understand this relationship, consider the following example. Assume that you define and initializeInt field:

Int counter = 0;

Thread a and thread B share the counter field. Suppose thread a adds counter:

Counter ++;

Then, thread B prints the printer:

System. Out. println (Counter );

If the preceding two statements run in the same thread, the thread is secure. Assume that the printed counter value is 1. However, if the two statements are executed in different threads, the printed counter value may be 0. Because this does not ensure that thread a's change to counter is visible to thread B, unless the programmer has established a previous (happens-before) Relationship between the two threads.

There are several actions on the relationship before the creation. One of them is synchronization, as we will see below.

We can see the two actions of the relationship before the creation.

When a statement calls thread. Start, each statement that has a lifetime relationship with this statement also has a lifetime relationship and each statement that executes a new thread. Create a new threadCodeThe effect is visible to this new thread.

When a thread reaches the critical period and causes the thread. Join method to be in another thread, it returns. Then, all the statements executed by the thread in the critical period establish a pre-relationship with all the statements that subsequently join. The effect of the code of this thread is now visible to the threads that have completed the join operation.

 

Synchronized Methods

Java provides two basic synchronization syntaxes: the synchronous method and the synchronous statement block.

To synchronize a method, simply add the synchronized keyword to the method declaration.

 
Public class synchronizedcounter {private int C = 0; Public synchronized void increment () {C ++;} public synchronized void decrement () {c --;} public synchronized int value () {return C ;}}


Assume that count isSynchronizedcounter:


First, the call of the two synchronous methods on the same object cannot be staggered. When a thread is executing a synchronous method for an object, all other threads that call this object are blocked,

Knowing that this thread has processed this object.

Second, when a synchronization method exits, it will automaticallyCall of the callback for the synchronization method of the same objectCreate a previous relationship. This ensures that the state of this object changes to all threads.

Is visible.

The builder cannot be synchronized. It is a syntax error to use the synchronized keyword on the builder. It makes no sense to synchronize the builder. Because, when creating an object, only the thread that creates the object

You can access him.

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.