当前位置:网站首页>Three usages of synchronized keywords in vernacular
Three usages of synchronized keywords in vernacular
2022-07-24 00:55:00 【wzf6667】
synchronized Scope of keyword
synchronized There are two scopes of keywords :
Within an object instance ,synchronized aMethod(){} It can prevent multiple threads from accessing this object at the same time synchronized Method ( If an object has more than one synchronized Method , Just one line Cheng visited one of them synchronized Method , Other threads cannot access any of this object at the same time synchronized Method ). At this time , Of different object instances synchronized The method is non-interference . in other words , Other threads can also access... In another object instance of the same class at the same time synchronized Method ;
The scope of a class ,synchronized static aStaticMethod{} Prevent multiple threads from accessing... In this class at the same time synchronized static Method . It works on all object instances of a class .
Popular speaking , You can lock an instance object , Get the instance object to run , The other is locking a class , It is equivalent to locking all instance objects , Prevent multiple threads from accessing... In this class at the same time synchronized static Method .
synchronized The three main ways to use keywords :
Decorated instance method : Lock the current object instance , Get the lock of the current object instance before entering the synchronization code . It's like all methods of an object vie for the right to use the object , Once a method grabs the lock , To run this method . And the other parts of this object synchronized Method cannot be run at this time .
Modified static method : That is to lock the current class , It will affect all object instances of the class , Because a static member does not belong to any instance object , It's a class member ( static Indicates that this is a static resource of this class , No matter new How many objects , Only one copy. ). So if a thread A Calling an instance object is not static synchronized Method , And threads B You need to call the static state of the class to which this instance object belongs synchronized Method , Permissible , There will be no mutual exclusion , Because accessing static synchronized The lock occupied by the method is the lock of the current class , Access is not static synchronized The lock occupied by the method is the current instance object lock .
Decorated code block : Specify the lock object , Lock a given object , Get the lock of the given object before entering the synchronization code base . Given a lock object , Only by grabbing this lock can it run .
To sum up
synchronized Keywords added to static Static methods and synchronized(class) All the code blocks are for Class Class lock .synchronized Keyword is added to instance method to lock object instance . Try not to use synchronized(String a) because JVM in , String constant pool has caching function !
边栏推荐
- How to relieve the pillow quickly
- Summary of pit websocket
- How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
- Notes and Reflections on the red dust of the sky (V) strong culture and weak culture
- T-seda code
- Coloring old photos - deoldify get started quickly
- Bean Validation使用篇----05
- vim常用命令
- 采坑websocket总结
- Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
猜你喜欢

入职3个月的测试员面临转正,领导:1年工作经验包装成5年,试用期淘汰

落枕如何快速缓解

Classic examples of C language - adding two scores

Creo 9.0 mouse button operation for model observation

There are various signs that apple is expected to support AV1

Development of main applet for business card traffic near the map

Seektiger's okaleido has a big move. Will the STI of ecological pass break out?

【LeetCode第 83 场双周赛】

Project scenario: NVIDIA SMI unable to datemine the device handle for GPU 0000:01:00.0: unknown error

How to relieve the pillow quickly
随机推荐
入职3个月的测试员面临转正,领导:1年工作经验包装成5年,试用期淘汰
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Notes and Thoughts on the red dust of the sky (VI) due to lack
Implementation of singleton mode and prevention of reflection and serialization
Classic examples of C language - adding two scores
Development of main applet for business card traffic near the map
Network system experiment: solve the problem of Ping failure
暑假第四周总结
Focus on microservices
【LeetCode第 83 场双周赛】
Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi
Expérience du système réseau: résoudre les problèmes de ping
Tutorial on principles and applications of database system (043) -- MySQL query (V): Sorting Query Results
Résumé du websocket minier
What is promise? What are the benefits of promise
Printf function - conversion description
JS drag and drop element
《天幕红尘》笔记与思考(六)因缺而需
Bean Validation使用篇----05