Use of synchronization lock for Android multithreading

This article mainly introduces the use of Android multithreading synchronization lock and shares it with you, as follows:

1、 Synchronization mechanism keyword synchronized

For Java, the most commonly used synchronization mechanism is the synchronized keyword, which is a rough language based lock that can act on objects, functions and classes. Each object has only one lock. Whoever can get the lock has access rights. When synchronized acts on a function, the locked object is also the object of the class in which the function is located. When synchronized acts on a class, it is the class of the lock, not a concrete object.

The above demonstrates the synchronization method, synchronization block, synchronization class object and synchronization static method. The first two locks are objects, and the last two locks are class objects. For the class object, its function is to prevent multiple threads from accessing the synchronized lock added code block at the same time, while the synchronized function on the reference object is to prevent other threads from accessing the synchronized code block or function in the same object.

2、 Show locks - reentranklock and condition

Reentranklock implements the same semantics as the built-in lock synchronized, but has higher flexibility.

(1) Flexibility of acquisition and release. (2) Rotation lock and timing lock. (3) Fairness.

Basic operation:

Lock(): get lock

Trylock(): attempt to acquire lock

Trylock (long timeout, timeunit unit): attempts to acquire a lock. If it cannot be acquired within the specified time, it will timeout.

Unlock(): release the lock

Newcondition(): obtain the condition of the lock

The general combination of reentrantlock is lock, trylock and unlock. It should be noted that do not forget to call unlock to release the lock, which is responsible for the problems that may cause deadlock. The common forms of reentrantlock are as follows:

It should be noted that the lock must be released in finally open. Otherwise, if the protected code throws an exception, the lock may never be released!!

There is also an important function newcondition () in the reentrantlock class, which enables the user to obtain a condition on lock (), that is, condition is bound to lock. Condition is used to realize the communication between threads. It is used to solve the problem that object. Wait(), nofityall() is difficult to use. The method of condition is as follows:

Await(): thread waiting

Await (int time, timeunit unit) thread waits for a specific time, and the time exceeded is timeout.

Signal() wakes up a waiting thread randomly

Signal() wakes up all waiting threads

Example code:

Execution results:

3 24 no data yet, wait

3、 Semaphore

Semaphore is a counting semaphore, and its essence is a "shared lock". Semaphores maintain a semaphore permission set, and threads can obtain semaphore permission by calling acquire (). When there is a license available in the semaphore, the thread can obtain the license; Otherwise, the thread must wait until a license is available. A thread can release its semaphore license by releasing ().

Example:

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
分享
二维码
< <上一篇
下一篇>>