Java multithreading – thread synchronization and locking

1、 Synchronization issues raised

Thread synchronization is to prevent data damage caused by multiple threads accessing a data object.

For example, two threads, threada and threadb, operate on the same foo object and modify the data on the foo object.

Operation results:

Thread-b: x value of current foo object = 40 thread-a: x value of current foo object = 40 thread-b: x value of current foo object = - 20 thread-a: x value of current foo object = - 20 thread-b: x value of current foo object = - 80 thread-a: x value of current foo object = - 80

It is found from the results that such an output value is obviously unreasonable. The reason is that two threads access the foo object and modify its data without control.

If you want to maintain the rationality of the results, you only need to achieve one goal, that is, restrict the access to foo, and only one thread can access it at a time. This will ensure the rationality of the data in the foo object.

In the specific java code, you need to complete the following two operations:

Identify the resource class foo variable X of competitive access as private;

Synchronize the code of which variables are modified, and use the synchronized keyword to synchronize the method or code.

Thread a ends running and decreases by "10". The current value is 90. Thread C ends running and decreases by "- 3". The current value is 93. Thread B ends running and decreases by "- 2". The current value is 95. Thread D ends running and decreases by "5". The current value is 90

2、 Synchronization and locking

1. Principle of lock

Every object in Java has a built-in lock.

When a program runs on a non static synchronized synchronization method, it automatically obtains the lock related to the current instance (this instance) of the executing code class. Obtaining the lock of an object is also called obtaining the lock, locking the object, locking on the object, or synchronizing on the object.

The object lock only works when the program runs to the synchronized synchronization method or code block.

An object has only one lock. Therefore, if a thread acquires the lock, no other thread can acquire the lock until the first thread releases (or returns) the lock. This also means that no other thread can enter the synchronized method or code block on the object until the lock is released.

Releasing a lock means that the locking thread exits the synchronized synchronization method or code block.

There are several key points about lock and synchronization:

1) . only methods can be synchronized, but variables and classes cannot be synchronized;

2) . each object has only one lock; When it comes to synchronization, you should know on what? That is, on which object?

3) . it is not necessary to synchronize all the methods in the class. The class can have synchronous and asynchronous methods at the same time.

4) If two threads want to execute the synchronized method in a class, and the two threads use the same instance to call the method, only one thread can execute the method at a time, and the other needs to wait until the lock is released. That is, if a thread obtains a lock on an object, no other thread can enter any synchronization method in the class.

5) If a thread has synchronous and asynchronous methods, the asynchronous methods can be freely accessed by multiple threads without being restricted by locks.

6) When a thread sleeps, any lock it holds will not be released.

7) Threads can obtain multiple locks. For example, if you call the synchronization method of another object in the synchronization method of one object, you get the synchronization lock of the two objects.

8) Synchronization damages concurrency, and the scope of synchronization should be reduced as much as possible. Synchronization can synchronize not only the whole method, but also a part of the code block in the method.

9) When using the synchronization code block, you should specify which object to synchronize on, that is, which object's lock to obtain. For example:

Of course, the synchronous method can also be rewritten as an asynchronous method, but the functions are exactly the same, for example:

And

The effect is exactly the same.

3、 Static method synchronization

To synchronize static methods, you need a lock for the entire class object, which is the class (XXX. Class).

For example:

Equivalent to

4、 What happens if a thread cannot get a lock

If a thread attempts to enter a synchronous method and its lock is already occupied, the thread is blocked on the object. In essence, a thread enters a pool of the object and must wait until its lock is released and the thread becomes runnable or runnable again.

When considering blocking, it is important to note which object is being used for locking:

1. Threads that call non statically synchronized methods in the same object will block each other. If there are different objects, each thread has its own object lock, and threads do not interfere with each other.

2. Threads calling static synchronization methods in the same class will block each other, and they are all locked on the same class object.

3. Static synchronous methods and non static synchronous methods will never block each other, because static methods are locked on class objects and non static methods are locked on objects of that class.

4. For synchronized code blocks, you should see what objects have been used for locking (the contents in parentheses after synchronized). Threads synchronized on the same object will block each other, and threads locked on different objects will never block each other.

5、 When do I need to synchronize

When multiple threads access mutually exclusive (exchangeable) data at the same time, they should be synchronized to protect the data and ensure that two threads will not modify it at the same time.

Data that can be changed in non static fields is usually accessed using non static methods.

Data that can be changed in static fields is usually accessed using static methods.

If you need to use static fields in non static methods or invoke non static methods in static fields, the problem will be very complicated. It is beyond the scope of sjcp examination.

6、 Thread safe class

When a class is well synchronized to protect its data, this class is called "thread safe".

Even for thread safe classes, you should be very careful, because the threads of operation are still not necessarily safe.

7、 Thread synchronization summary

1. The purpose of thread synchronization is to protect resources from damage when multiple threads access a resource.

2. The thread synchronization method is implemented through locks. Each object has only one lock. This lock is associated with a specific object. Once the thread obtains the object lock, other threads accessing the object can no longer access other synchronization methods of the object.

3. For static synchronization methods, the lock is for this class, and the lock object is the class object of this class. The interlocking of static and non-static methods does not interfere. A thread obtains locks. When a synchronization method on another object is accessed in one synchronization method, it will obtain these two object locks.

4. For synchronization, you should always be aware of which object to synchronize on, which is the key.

5. When writing thread safe classes, you should always pay attention to making correct judgments on the logic and safety of multiple threads competing to access resources, analyze the "atomic" operation, and ensure that other threads cannot access competing resources during atomic operation.

6. When multiple threads wait for an object lock, the thread that does not acquire the lock will block.

7. Deadlock is caused by threads waiting for each other. In practice, the probability of deadlock is very small. Really let you write a deadlock program, not necessarily easy to use, ha ha. However, once the program deadlock occurs, the program will die.

Original link: http://www.cnblogs.com/linjiqin/p/3208843.html

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>