From the java memory model, we know that each thread has its own thread stack, and local variables are placed in each thread's thread of its own threads.
And other threads cannot access these local variables.
So, in this case, should we synchronize the local variables?
There are two situations:
int
double
ArrayList
, ( )
, . , (), , .
: static , .
static
, , , , , .
:
public class MyClass { public void myMethod() { //Assume Customer is a Class Customer customer = getMyCustomer(); synchronized(customer) { //only one therad at a time can access customer object which ever holds the lock } } }
customer , customer ( ).
customer
Java ( , ), - .
, , (-), , , , . - , therad .
, . , .
: , . , , -, .
, : , , .
, , "" . , :
void foo() { final Object lock = new Object(); Thread a = new Thread() { uses lock Thread b = new Thread() { uses lock
, "" . : , . . .
Yes, it makes sense when a local variable is used to synchronize access to a block of code from threads that are defined and created by the same method as the local variable.
Source: https://habr.com/ru/post/1673686/More articles:Remove comments from a text file in Java - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1673682/shell-access-to-persistent-volume-in-google-cloud&usg=ALkJrhjoxGGODOvGyiZII6SHkd0HmR3xcAGoogle Chrome dev tools don’t display website on developer machine (when remote debugging Android device) - google-chrome-devtoolsPandas dataframe format for json - jsonHow to check positional notes of notes in format strings using msgfmt? - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1673687/event-data-always-seems-to-be-null-on-onwrite&usg=ALkJrhhnEBJxz5k6boGrLtneW2_2Tlxa5Aquestions about pandas.to_sql - pandasUIWindow height control on call - iosAngular Translation sometimes not translated with one-time binding - javascriptКак добавить флаг -l "(ell) компилятора в CMake - c++All Articles