stack closure

stack closure (local variables)

The stack limit is a special case of thread closure. Objects can only be accessed through local variables. Local objects are restricted to the execution thread and exist in the execution thread stack. Other threads cannot access this stack, thereby ensuring thread safety. (Each thread has a working memory, the working memory includes a stack, the local basic type variables are in the stack, the reference type reference is in the stack, and the object pointed to by the reference is in the heap). 
Examples of stack limits are as follows:

 

To understand stack closure, you need to first understand what data is shared among multiple threads and what data is not shared

 

**Multi-threaded data sharing mechanism

What resources do threads in the same process share, and which resources do they share exclusively?
Shared resources are
a. Heap Since the heap is opened up in the process space, it is shared as a matter of course; so the new ones are shared (the global heap and the local heap are divided on the 16-bit platform, and the local heap is exclusive)
b. Global variables are not related to a specific function, so they are not related to a specific thread; therefore, they are also shared
c. Although static variables are "placed" in a function in the code for local variables, their storage locations are the same as global variables. They are stored in the .bss and .data sections opened up in the heap, which are shared of
d. Common resources such as files are shared, and threads using these common resources must be synchronized. Win32 provides several ways to synchronize resources, including signals, critical sections, events, and mutexes.
Exclusive resources are
a. The stack is exclusive
b. Register This may be misunderstood, because the computer's registers are physical. Isn't the value of each thread different? In fact, the thread is stored in a copy, including the program counter PC

             

  

Therefore, the reference to the TreeSet object is kept in the annimal variable,

   animal is again a local variable

   Local variables are stored on the stack

   Each thread has its own stack

   Therefore, different threads have different stacks, and different threads cannot operate the same TreeSet object at the same time, thus ensuring security.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127551&siteId=291194637