Java multi-threaded program design points

xiaoxiao2021-03-06  95

http://blog.9cbs.net/duoshanx/archive/2004/08/25/84846.aspx

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 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 is waiting for some IO operation (2) thread attempt to call another object's "synchronization" method, 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, 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) will result Give the variable I. (6) Save i to the thread local work 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. Due to 13 reasons, our solution is: Class XXX Extends Thread {// I will often modify 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 the method of real-time communication between the two threads is taken from steps: (1). Create a PiPedWriter and a pipe between PiPedReader and the pipes between them; PiPedReader IN = New PipedReader (New PiPedWriter ()) (2). In need Before sending information, the external PIPEDWRITER is guided to its internal WRITER instance OUT (3). Before the thread that needs to accept information, the external PiPedReader is switched to the Reader instance in (4). All things 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.

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

New Post(0)