1. What does thread safety mean? is the servlet thread-safe?
A: If your code is in a process where more than one thread is running at the same time, these threads may run this code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe. Or, the interface provided by a class or program for a thread is an atomic operation or a switchover between multiple threads does not result in ambiguity in the execution of the interface, that is, we do not have to consider the problem of synchronization.
Servlets are not thread-safe, each servlet is instantiated only once, each invocation is the same instance of the servlet, and the class variable is not thread-safe and the data is large enough to be exposed to an exception.
2. How does synchronization have several implementations?
Answer: There are two implementations of synchronization, namely Synchronized,wait and notify
3. What is the use of volatile? Can you describe the volatile application scenario in a sentence?
A: Volatile variables have synchronized visibility characteristics, but they do not have atomic properties. Can be seen as a "lesser synchronized"; volatile variables require less coding and run-time overhead than synchronized blocks, but the functionality they can achieve is only part of the synchronized.
You can use volatile variables instead of locks in a limited number of situations. For the volatile variable to provide ideal thread safety, the following two conditions must be met:
A. Writes to a variable do not depend on the current value.
B. The variable is not included in the invariant with other variables.
4. Please describe the memory model and workflow of the following Java.
A: Java divides memory into two types: one is stack memory, and the other is heap memory.
Stack memory: Storing objects: Variables of basic types in functions and reference variables of objects, static class methods; Features: Stack has a very important particularity, is that the data in the stack can be shared.
Heap Memory: Storage object: Used to hold objects and arrays created by new; feature: the memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.
Java memory model: According to the instructions in Java Language specification, there is a main memory in the JVM system (main memories or Java Heap memory), and all object members in Java Variables are stored in main memory and are shared for all threads. Each thread has its own working memory (working memory), which is a copy of some of the object member variables in main storage, and the thread's operation on all object member variables is done in working memory, the threads cannot be accessed directly from each other, and the variables are passed through the main store.
(1) Get the lock of the object Monitor (lock)
(2) Emptying working memory data, copying object member variables from main memory to current working memory, i.e. synchronizing data (read and load)
(3) Execute code, change shared variable value (use and assign)
(4) Brush the working memory data back to main storage (store and write)
(5) Releasing the lock on the object monitor (unlock)
Java face question-thread safety