Java multi-threading to produce a simple case of deadlock

synchronized is a keyword in Java, it is a synchronization lock. It modified objects are the following:

1. modifying one code block , the modified code blocks called sync blocks of statements , the scope of action of which is enclosed in braces {} of code, an object of this action is to call the object code block;
2. a modified method , to be modified method is called synchronization method , the whole range of action of the method, the object of this action is to call an object method;
3. modify a static method , the scope of which is to effect the overall static method , the object is the role this class of all objects;
4. modify a class , its role is synchronized range bracketed portion of the back, the role of the main objects of this class are all objects.

First , we create two strings, strA, strB in java. Let each thread with synchronized lock the string (thread A first lock strA, go lock strB; thread B to lock strA, then lock strB), if thread A locked strA, thread B lock strB, A thread on no way to lock strB, there is no way to lock the thread B strA, this time into a deadlock.

Guess you like

Origin www.cnblogs.com/abcdjava/p/11028951.html