Java multi-threaded interview, written test direction

1. ThreadLocal class
     thread-level local variables, providing an independent variable copy for each thread that uses the variable, and each thread modifying the copy does not affect the copies of other thread objects.
   ThreadLocal instances usually appear in a class as static private fields.
2. What will throw InvalidMonitorStateException? When
     calling any method in Object.wait()/Object.notify()/Object.notifyAll(), if the current thread does not acquire the lock of the object, then IllegalMonitorStateException will be thrown The exception of
3.Object.wait() and Thread.sleep()
     Object.wait(), in the case of owning the object lock, release the object lock. The method returns once the thread reacquires the lock on the object.
     Object.notify()
     Thread.sleep(): Gives up the execution time allocated by the CPU, but does not release the locks owned by the thread.
4. Using synchronization on static methods
     If a static method is declared as synchronized, it is equivalent to calling synchronized(class.class) on this method.
   When a thread enters a synchronized static method, other threads cannot enter any static synchronized method of this class.
5. Deadlock
     Deadlock means that two or more threads are blocked indefinitely, and the threads wait for each other for the required resources.
     It may happen in the following situations:
          when two threads call Thread.join() to each other;
          when two threads use nested synchronization blocks, one thread occupies the necessary lock of the other thread, and it may die when blocked while waiting for each other Lock.
6. ReentrantLock:
     Waiting can be interrupted: tryLock (long timeout, TimeUnit unit) When the thread holding the lock does not release the lock for a long time, the waiting thread can choose to give up waiting and deal with other things instead.
     Fair lock: When multiple threads are waiting for the same lock, they must acquire the lock in sequence according to the sequence of events that apply for the lock; when an unfair lock is released, any thread waiting for the lock has the opportunity to acquire the lock.
The locks in Synchronized are unfair locks, and ReentrantLock is also an unfair lock by default. tryLock() breaks fairness, returns true immediately if the lock is not held by another thread, and sets the lock's hold counter to 1.
     Locks bind multiple conditions: A ReentrantLock object can bind multiple Condition objects at the same time.
7. Thread status:
     new NEW:
     running RUNNABLE:
     waiting indefinitely WAITING: waiting for other threads to wake up explicitly.
          Object.wait() without Timeout parameter; Thread.wait() without Timeout parameter.
     Waiting for TIMED_WAITING: The system will automatically wake up after a certain period of time.
          Object.wait() to set Timeout parameter; Thread.wait() to set Timeout parameter; Thread.sleep() method.
     Blocking BLOCKED: Waiting to acquire an exclusive lock, waiting to enter a synchronization area.
     END TERMINATED:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326349729&siteId=291194637