The difference between synchronized and lock this question we do not know whether to encounter, many friends see this question, mouth can buzzed two words, and then lose their temper, and the interviewer looked at each other, and then a cool BGM sounded in the ear. Here is shiyao sauce to answer this question for you
1. Threads and processes: First of all, let’s learn about threads and processes. Processes have a larger scope than threads and have independent resource allocation scheduling. The thread is the process’s younger brother and has no choice but to share resources with other threads.
2. Several important methods of Thread: Start ——– : the run method is automatically called after the start method runs. Run ——— : The run method is automatically called after the start method runs. Run thread stop——– force to terminate thread join—— after a call, wait for the called method to finish, equivalent to jumping the queue
What about wait() and notify()? Wait () and notify() are Object methods, not Thread methods. Meanwhile, wait() and notify() are used together to indicate thread suspension and thread recovery, respectively.
One more common problem, by the way: the difference between wait() and sleep() is that simply wait() releases the object lock while sleep() does not. There is a lot of information on these issues and I won’t go into them again.
3, thread state: a thread has 5 states new — ready — run — blocked — dead
New state: Create a thread object without calling the start() method
Ready: A thread enters the ready state when the start() method is called, but it does not immediately become the current thread when the start() method is called. It is ready until it becomes the current thread. It is worth mentioning that threads will also enter the ready state when they recover from sleep and suspension.
Running status: The thread is set to the current thread and starts executing the run() method. That’s when the thread enters the running state
Blocked: A thread is suspended, for example after calling sleep()
Dead: the thread is finished
4. Lock type
Reentrant lock: All synchronized methods in the execution object do not acquire the lock again
Interruptible lock: Interruptible while waiting to acquire the lock
Fair lock: The lock is acquired according to the waiting time of the thread. If the waiting time is long, the thread has the priority to obtain the lock
Unfair lock: A lock is acquired randomly
Read/write lock: Read and write resources are divided into two parts. Multiple threads can read and write resources simultaneously
Hierarchy of existence: Synchronized is a Java keyword, and Lock is a class at the JVM level
Lock in finally, the lock must be released; otherwise, the thread will likely be locked
Synchronized assumes that thread A acquires the lock and thread B waits. If thread A is blocked, thread B will wait for A Lock, depending on the situation, and Lock can be acquired in multiple ways, basically, the thread can try to acquire the Lock without waiting forever
Synchronized Reentrant uninterruptible non-fair Lock reentrant judge fair Lock
Applicable scope: synchronized A small amount of synchronization Lock A large number of synchronization
Lock详细介绍与Demo
public interface Lock {
/**
* Acquires the lock.
*/
void lock();
/**
* Acquires the lock unless the current thread is
* {@linkplain Thread#interrupt interrupted}.
*/
void lockInterruptibly() throws InterruptedException;
/**
* Acquires the lock only if it is free at the time of invocation.
*/
boolean tryLock();
/**
* Acquires the lock if it is free within the given waiting time and the
* current thread has not been {@linkplain Thread#interrupt interrupted}.
*/
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
/**
* Releases the lock.
*/
void unlock();
Copy the code
} From the Lock interface we can see that there is a main method, the function of these methods can be seen from the comments: Lock () : obtains the Lock, if the Lock is temporarily used wait
Unlock () : the lock is released
TryLock (): Note that the return type is Boolean, false if the lock was occupied when it was acquired, true otherwise
TryLock (long time, TimeUnit unit) : A time period is guaranteed to wait parameter time compared to tryLock()
LockInterruptibly () : If the thread is waiting to acquire the lock, it can be interrupted to do something else first
In the previous section, “lock type (lockInterruptibly())”, “lock status (tryLock())”, and the process I used “yes” when I wrote “it is possible to try to obtain the lock so that the thread does not wait forever”.
The synchoronzied base layer has also been optimized
Thread spin and adaptive spin
2. Lock elimination
3, lock coarsening
4. Lightweight locks
5, biased lock