After JDK5, Java provides a more convenient security mechanism that uses lock locks for thread-locked operations and release operations without using synchronized . More clear. Example:
Lock lock=new Reentrantlock ();
Lock.lock
Code that needs to be locked
Lock.unlock
Why is lock more secure? Because synchronized may cause deadlock phenomenon, specifically about the deadlock phenomenon can refer to the operating system, there is no more to say.
Summarize the main problems in the thread
First,
There are several ways to implement multithreading
There should be three, the first inherits the thread class, and the second is the implementation of the Runnable interface. The third is related to the thread pool.
There are several ways to synchronize
Synchronous code blocks and synchronous methods, where several synchronization methods are divided into static and non-static, non-static lock objects using this can, static use. Class.
Start a thread is run () or start ()?
Using Run is really just a simple method call, regardless of thread, and using Start (), a Java Virtual machine creates a thread that executes the run method with that thread.
The difference between sleep () and wait ()
Sleep must contain the time needed for sleeping and do not need to wake up, while wait is locked and needs to be awakened.
Java Learning Day 22nd (about lock lock in Java and multi-threaded interview questions)