Java multithreading and static methods, java multithreading static

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.