Java multithreading and static methods, java multithreading static
Java does not have the Foundation to develop Android applications and finds that it does not have sufficient understanding of static methods. The following problems exist:
InMultithreadingUsed inStatic MethodWhat will happen? That is to sayMultithreadingAccessStatic MethodWhat will happen? Will happen?Thread security issues?
Reference: http://my.oschina.net/keyven/blog/219603
Conclusion:
1. When the same static method is used in multiple threads, each thread usesRespectiveOfCopy of the instance field, AndShareOneStatic field). Therefore, if the static method does not operate a static memberInternal MethodUseInstance field)Does not cause security issues.
2. AddSynchronizedThe static method of the keyword is calledStatic Synchronization Method.
The "Class" Object of the Class is obtained when you access the static synchronous method. Therefore, when a thread enters the static synchronous method, the thread monitor obtainsObject lock,Other threads cannot access any static synchronization method of this class.. Unlike the instance method, because multiple threads can simultaneously access different instance synchronization instance methods. This is actuallyUse semaphores to achieve process mutex and SynchronizationIfSame classInMultiple Static MethodsTo process multi-threaded shared data.Use semaphores to solve producer-consumer problems. That is to say, the static method is a copyCritical resources, Access to static methods belongsEnter critical sectionThe modification to static variables is a critical resource, and the modification to static variables belongs to the critical section.
3, the difference between synchronized and static synchronized reference: http://blog.csdn.net/baple/article/details/12080597
Other multithreading problems:
Reference: http://www.lupaworld.com/article-218037-1.html
Mainly:
1. multi-threaded synchronization refers to access control for shared resources;
2. The implementation methods include inheriting Thread and Runnable. We recommend that you use Runnable to facilitate multiple inheritance;
3. When the static synchronization method enters, the thread monitor obtains the object lock of the class where the method is located.