Multi-threaded design points

xiaoxiao2021-03-06  52

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 state of thread has a few status of 'Ready', 'Running', 'Sleeping', 'Blocked', and 'Waiting',

'Ready' indicates that the thread is waiting for the CPU to assign the time to be allowed.

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 synchronized when accessing the same resource.

5. Each thread will register yourself, there is actually a reference to it, so the garbage collection mechanism 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 has several reasons:

(1) Thread waiting for some IO operation

(2) The thread attempts to call another object's "synchronization" method, but that object is locked, temporarily unable to use.

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, which belongs to the original variable:

(1) Read the I value from the main memory to the 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) The result is given 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 reading of steps 1 to 2, and the value of I = I 1 is also executed simultaneously (in step 4). .

Double and long variables are non-ATOMICs. The array is an Object non-atomic shape.

14. For 13 reasons, our solution is:

Class XXX EXTENDS THREAD {

// I will be modified frequently

Private INT i;

Public synchronized int} {returni}

Public synchronized void update () = i 1;}

........

}

15. Volatile variable, volatile variable indicates that it must be consistent with the main memory, it is actually "synchronization of variables", that is, for the operation of the Volatile variable is atomic, such as before the long or double variable. 16. Use Yield () to automatically discard the CPU, sometimes more improved than SLEEP.

17. The difference between SLEEP () and WAIT () is that the wait () method is called when it is called, but we can use it just in a synchronized method or code block.

18. By manufacturing a reduced synchronization range, the code block synchronization is achieved as much as possible, WAIT (Mix) can exit Wait at the specified millisecond; for Wait () needs to be woken up by Notisfy () or NotifyAll ().

19. Constructing a method of real-time communication between two threads:

(1). Create a PIPEDWRITER and a PiPedReader and the pipeline between them;

PipedReader in = New PipedReader (New PiPedWriter ())

(2). Before the thread that needs to send information, the external PiPedWriter will give the external PiPedWriter to the Writer instance OUT.

(3). Before you need to accept the thread of the information, the external PiPedReader is guided to the Reader instance in it.

(4). All things that are put into OUT can be extracted from in.

20. Synchronized has a decline in the problem that the biggest shortcoming will bring deadlocks, only to prevent deadlocks by cautious design, and there is no way, this is a reason why threads are difficult to tame. Don't use STOP () Suspend () Resume () and Destory () method

21. When a large number of threads are blocked, the highest priority thread runs first. But do not mean that the low-level thread will not run, and the probability is small.

22. The main advantage of the thread group is that the operation of the entire thread group can be done using a single command. A thread group is rarely needed.

23. Enhance multithreading performance from the following aspects:

Check all possible blocks of Block, using Sleep or Yield () and Wait () as much as possible.

Extend the time of SLEEP (millisecond) as possible;

Running threads do not have to exceed 100, and it is not too much;

Different platforms Linux or Windows and different JVM run performance are very different.

24. Recommend a few related English articles:

Use threading tricks to improve Programs

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

New Post(0)