1. Synchronized Function
Usage:Synchronized Foo (){}
The literal meaning is to keep a function block synchronized, but to whom? The answer is to use another or some functions with the synchronized keyword. It ensures that only one function with synchronized is running at the same time in this object, and only run in a thread. If these functions may be run in different threads and access the same resource at the same time, you can use them. (I don't know if I'm right about this passage.) only
One thread at a time can call a Synchronized Method for a participant object (although that thread can call more than one of the object's synchronized methods). Objects
Can have synchronized methods that prevent threads from accessing that object until the synchronization lock is released.
For example:
Synchronized foo0 () {;}
Synchronized foo1 () {B ;}
If you want to synchronize data between A and B (that is, when a is executed, B cannot execute B), you need to add synchronized to both functions. Adding only one function is equivalent to not adding.
If you synchronize only one of the methods, then the other is free to ignore the object lock and can
Be called with impunity. This is an important point: Every method that accesses a critical shared resource must be synchronized or it won't work
Right.
2. Synchronized object
Usage:
synchronized(syncObject) { // This code can be accessed // by only one thread at a time}
The literal meaning is to synchronize an object. In fact, an object is used as a lock to create a critical section. The code in the critical section can only be accessed by one thread at a time. Further, syncobject has only one key. At a time point, the thread that gets the key can enter the critical section and return the key to syncobject after execution, other threads without keys wait until the key is idle and the system determines who to give the key.
Note: Misuse of the synchronized method will reduce the system performance, because more operations will be performed when you enter or exit the synchronization method (Block), and you should try to use it as little as possible.
In addition, to access the arraylist of another thread in a non-synchronous thread, clone one copy:
ArrayList lv = null; // Make a shallow copy of the List in case // someone adds a listener while we're // calling listeners: synchronized(this) { lv = (ArrayList)actionListeners.clone(); }
Among them, synchronized (this) {} is equivalent to synchronized Foo () {}, that is, using the large lock of the current object to synchronize. PS synchronized does not inherit from the quilt class.
Basic thread knowledge: four states of a thread: 1 new 2 runnable can be run at any time, sometimes run, sometimes 3 dead does not run normally occurs after run {} Exits 4 blocked
3 how to block a thread?How does one block a thread? The purpose is to use blocking to achieve a certain degree of synchronization. There are two types of blocking: Sleep and wait/notify. Difference: Sleep does not release the lock of the current thread object, while wait releases the lock of the current thread, Which is re-retrieved by notify. After the lock is retrieved, the program after wait () is executed. Precautions for using wait/notify: because these two functions need to operate on the lock of the current object, they cannot be used as "Rice-free". They need to be obtained before the operation lock, but when can we make sure we have a lock in our hands? The answer is that in the synchronized synchronization block (method), the synchronization block is synchronized (Object O) {}, which ensures that the object lock is obtained, and the synchronization method is synchronized void Foo () {}, which can be used to obtain the Lock of some function objects. To sum up the above experience, we should call wait ()/notify () as follows ():
synchronized (object) { ... try { object.wait(); } catch(InterruptedException e) { } //object.notify(); ...}synchronized void foo () { ... try { wait(); //Current thread wait(); } catch(InterruptedException e) { } //object.notify(); ...}
Note the exception of catch thread interruption.
4 deadlockWhen thread a is blocked because of another thread B, thread B is blocked because of thread C, and thread C is blocked because of a, such an infinite loop causes a deadlock. Be careful when writing a program, because once a deadlock occurs, you will "die" for a long time to debug.
5. How to exit the thread5.1 use stop mark:
public void terminate() { stop = true; } public void run() { while (!stop) { ...... }}5.2 interrupt ():
// blocked is a thread
if(blocked == null) return; Thread remove = blocked; blocked = null; // to release it remove.interrupt();
Note: too many threads may lead to JVM stagnation. multithreading is not needed in linear scenarios. linear processes are faster than multithreading! Threads are generally used to increase user response speed and network download.