Reading Notes: Key Points of multi-thread programming

Source: Internet
Author: User
Reading Notes: Key Points of multi-thread programming:

1. multithreading can be divided into primary memory and working memory. In JVM, there is a primary memory dedicated to sharing data among all threads, and each thread has its own private working memory, the master memory and working memory are measured in the JVM stack and heap regions.

2. The thread states are 'ready', 'running', 'sleeping', 'blocked', and 'waiting,
'Ready' indicates that the thread is waiting for the time allowed by CPU allocation.

3. the thread running sequence is not run in the order we created them. The CPU processing thread sequence is uncertain. If you need to determine the sequence, you must manually intervene and use setpriority () method To set priority.

4. We have no idea when a thread will run. Synchronized is required when two or more threads access the same resource.

5. Each thread registers itself, and there is a reference to it somewhere. Therefore, the garbage collection mechanism is "helpless.

6. daemon thread difference the general thread is: once the main program ends, the daemon thread ends.

7. All synchronized methods in an object share a lock, which prevents multiple methods from performing write operations on the general memory at the same time. The synchronized static method can be locked to each other within the scope of a class.

8. All methods for accessing a key shared resource must be set to synchronized. Otherwise, it will not work properly.

9. If a known method does not cause a conflict, the most sensible method is not to use synchronized, which can improve the performance.

10. If a/"synchronous" method modifies a variable, and our method needs to use this variable (probably read-only), it is best to set this method as synchronized.

11. Synchronized cannot be inherited. If the method of the parent class is synchronized, its subclass overload method will not inherit "synchronization ".

12. There are several reasons for thread blocked congestion:

(1) The thread is waiting for some IO operations.
(2) The thread tries to call the "synchronization" method of another object, but the object is locked and cannot be used for the moment.

13. atomic operation (atomic): the operation on the primitive variable (primitive) is atomic. this means that these operations are thread-safe, but in most cases, we cannot use them correctly. Let's take a look at I = I + 1 and I is an int type, which is a primitive 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 copy.
(3) Load Variable 1.
(4) add I to 1.
(5) give the result to the variable I.
(6) save I to the local working copy of the thread.
(7) write back to the primary memory.

Note that atomic operations are limited to reading from step 1 to step 2 and writing from step 3 to step 2, the I value may still be interrupted by multiple threads that execute I = I + 1 at the same time (in step 1 ).

The double and long variables are non-atomic (non-atomic ). Arrays are non-atomic objects.

14. For 13 reasons, the solution is:

Class XXX extends thread {
// I will be modified frequently
Private int I;

Public synchronized int read () {return I ;}

Public synchronized void Update () {I = 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 to say, the volatile variable operation is atomic, for example, it is used before the long or double variable.

16. Using yield () will automatically discard the CPU, sometimes better than sleep.

17. the difference between sleep () and wait () is that the wait () method is unlocked when it is called, but what we can use is only in a synchronous method or code block.

18. reduce the synchronization scope through manufacturing and implement code block synchronization as much as possible. Wait (in milliseconds) can exit wait at a specified number of milliseconds; for wait (), it needs to be notisfy () or policyall () wake up.

19. The method of constructing real-time communication between two threads is divided into several steps:
(1) create a pipedwriter and a pipeline between pipedreader and them;
Pipedreader in = new pipedreader (New pipedwriter ())
(2). Before the thread that needs to send information starts, direct the external pipedwriter to its internal writer instance out.
(3) Before the thread that needs to receive information starts, direct the external pipedreader to its internal reader instance in
(4). In this way, the degree of everything that is put out can be extracted from in.

20. in addition to performance degradation, synchronized brings about the deadlock, which can be prevented only by careful design. There is no other way, which is also one reason why the thread is hard to tame. Do not use the stop () suspend () Resume () and destory () Methods

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

22. The main advantage of a thread group is that a single command can be used to complete operations on the entire thread group. Thread groups are rarely used.

23. Improve the Performance of multithreading in the following aspects:

Check all possible blocks and use sleep, yield (), and wait () as much as possible ();

Extend sleep (milliseconds) as much as possible;

There are no more than 100 running threads;

The Running Performance of Linux, windows, and JVM on different platforms varies greatly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.