Multi-threaded design points

zhaozj2021-02-16  79

1. There is a master memory and work memory in multi-threads. In JVM, there is a primary memory, which is responsible for all thread sharing data; and each thread has his own private work memory, main memory and work memory scores The Stack area of ​​JVM and the HEAP area. 2. The thread has a few status of 'Ready', 'Running', 'Sleeping', 'Blocked', and 'Waiting', 'Ready' indicates that the thread is waiting for the CPU allocation to allow operation. 3. Thread run order is not running according to the order when we create them, the order in which the CPU handling thread is uncertain. If you need to be determined, then you must manually intervene, use the setPriority () method to set the priority. 4. We don't know when a thread is running, two or more threads need synchronized5 when accessing the same resource. Each thread will register ourselves, there is a reference to it, so the garbage collection mechanism It is "helpless". 6. The DAEMON thread differences general thread is: Once the main program ends, the daemon thread will end. 7. All SYNCHRONIZED methods in an object share a lock, which prevents multiple methods from writing simultaneously for universal memory. The Synchronized Static method can be locked in a class range. 8. For all methods accessing a key shared resource, you must set them to synchronized, otherwise it will not work properly. 9. Suppose a method is known that a method does not cause conflicts, and the most sensible method is not to use Synchronized to improve some performance. 10. If one / "synchronization" method modifies a variable, and our method uses this variable (probably only read), it is best to set your own method to synchronized. 11. Synchronized cannot inherit, the method of the parent class is synchronized, then "synchronization" is not inherited in its subclass overload method. 12. Thread block blocked There are several reasons: (1) Threads waiting to call some of the "synchronization" method of another object, but that object is locked, temporarily unused. 13. Atomic operation (atomic), the operation of the primitive variable is atomic atomic. It means that these operations are threads, but in most cases, we don't use it correctly, come and see I = i 1, i is an INT type, belonging to the original variable: (1) Read the I value from the main memory to local memory. (2) Load the value from the local memory to the thread work copy. (3) Load variable 1 (4) Plus I plus 1. (5) to the variable I. (6) Save i to the thread local working copy. (7) Write back the main memory. Note that atomic operation is limited to the first step to 2 Step 6 and write, i = i 1 multi-thread interrupt (in step 4). Double and long variables are non-ATOMICs. The array is an Object non-atomic type.

转载请注明原文地址:https://www.9cbs.com/read-15864.html

New Post(0)