Benefit: Resolves a thread safety issue.
Cons: Relatively low performance because the lock needs to consume resources, resulting in deadlocks.
There are prerequisites for defining synchronization:
1, must have two or more than two threads, only need to synchronize.
2, multiple threads must be guaranteed to use the same lock.
The second manifestation of synchronization:
Synchronization function: In fact, the synchronization keyword is defined on the function, so that the function has synchronization.
Which lock does the synchronization function use?
By verifying that the function has its own object this, the lock used by the synchronization function is the this lock.
When the synchronization function is modified by static, which lock is used for synchronization?
The static function belongs to the class at the time of loading, and it is possible that the object is not produced by the class, but the bytecode file of the class is loaded into the memory and has been encapsulated as an object, which is the bytecode file object of the class.
So when static loading, only one object exists, then the static synchronization function uses this object.
This object is the class name. class
What is the difference between a synchronous code block and a synchronization function?
The lock used by the synchronization code block can be any object.
The lock used by the synchronization function is this, and the lock of the static synchronization function is the bytecode file object of the class.
There is only one synchronization in a class, and you can use synchronous functions. If you have multiple synchronizations, you must use a synchronous code block to determine the different locks. So the synchronization code block is relatively flexible.
-------------------------------------------------------
Question: Would you please write a singleton mode for lazy loading? Write lazy-style; how to solve when multi-threaded access occurs? To solve security problems and high efficiency? Not high; how to solve? Resolved in the form of double judgments.
Lazy type: Lazy loading mode.
When multi-threaded access to lazy-type, because of the lazy way of the common data in the operation of multiple statements. So it's easy to have thread safety issues. In order to solve, join the synchronization mechanism to solve security problems. But it has brought about a decrease in efficiency.
In order to solve the problem of efficiency, it is solved by double judgment.
Class single{
private static single s = null;
Private single () {}
public static single getinstance () {///lock is who? Byte-code file object;
if (s = = null) {
Synchronized (Single.class) {
if (s = = null)
s = new single ();
}
}
return s;
}
}
---------------------------------------------------------
Synchronous deadlocks: You can usually see the phenomenon as long as the synchronization is nested. There is a synchronous code block in the synchronization function, and a synchronization function in the synchronization code block.
Inter-thread communication: Ideas: Multiple threads are manipulating the same resource, but the action is different.
1: Encapsulates the resource as an object.
2: The task that the thread performs (the task is actually the Run method.) ) is also encapsulated as an object.
Wait for wake-up mechanism: the method involved:
Wait: The thread in the synchronization is frozen. Release the executive right and release the eligibility. Store thread objects in the thread pool at the same time.
Notify: Wakes one of the waiting threads in the thread pool.
Notifyall: All threads in the thread pool are awakened.
Attention:
1: These methods need to be defined in synchronization.
2: Because these methods must be marked to belong to the lock.
You have to know that the thread of a lock is wait, and that this thread is equal to the thread pool in a lock, only the notify of a lock can wake up.
3: All three of these methods are defined in the object class. Why is the method definition of an action thread in the object class?
Because these three methods all need to define the synchronization, and to identify the synchronization lock belongs to, since the lock is called, and the lock can be any object, the method can be called by any object must be defined in the object class.
Wait and sleep differences: Analyze both methods: from execution and lock to analysis:
Wait: You can specify a time or you can not specify a time. No time is specified and can only be awakened by the corresponding notify or Notifyall.
Sleep: The time must be specified to automatically turn from the frozen state to the running state (temporary blocking state).
Wait: The thread releases the execution and the thread releases the lock.
Sleep: The thread releases execution, but does not release the lock.
Thread stop: The thread can be stopped by means of the Stop method. But this way is out of date.
Stop thread: The principle is: let the thread run the code end, that is, the end of the Run method.
How do I end the Run method? The general Run method definitely defines loops. So just end the loop.
The first way: Define the closing tag for the loop.
The second way: if the thread is in a frozen state, it is impossible to read the tag, then it needs to be forced to clear its frozen state through the interrupt method in the thread class. Let the thread recover the status of the execution qualification, so that the thread can read the tag and end.
---------< Java.lang.Thread >----------
Interrupt (): Middle break thread.
setpriority (int newpriority): Change the priority of the thread.
GetPriority (): Returns the priority of the thread.
ToString (): Returns the string representation of the thread, including the thread name, priority, and thread group.
Thread.yield (): Pauses the currently executing thread object and executes other threads.
Setdaemon (TRUE): marks the thread as either a daemon thread or a user thread. Mark the thread as either a daemon thread or a user thread. When a running thread is a daemon thread, the Java virtual machine exits. The method must be called before the thread is started.
Join: You can use the Join method when you temporarily join a thread.
When a thread executes the Join method to the B thread. A thread is in a frozen state, releasing execution and B starting execution. A when will it be executed? Only when the B thread has finished running does the a resume running state from the frozen state.
Java Fundamentals (-----) Synchronization