Reading notes: Design Essentials of multi-thread programming

Source: Internet
Author: User
Tags garbage collection sleep volatile
Reading notes: Multi-threaded programming Essentials:

1. There are main and working memory points in multiple threads, and in the JVM there is a master memory that is dedicated to all threads sharing data, while each thread has his own private working memory, the main memory and the working memory decibels in the JVM's stack and heap areas.

2. The state of the thread has ' ready ', ' Running ', ' sleeping ', ' Blocked ', and ' waiting ' several states,
' Ready ' indicates the time at which the thread is waiting for the CPU allocation to run.

3. The thread run order is not run in the order in which we created them, the order of CPU processing threads is indeterminate, and if necessary, it must be manually intervened to set the priority using the SetPriority () method.

4. We do not know when a thread is running, and two or more threads need to synchronized when accessing the same resource

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

6. The daemon thread distinguishes the general thread: once the main program is finished, the daemon thread ends.

7. All synchronized methods in an object share a lock that prevents multiple methods from writing concurrently to the common memory. The synchronized static method can be locked together within a class scope.

8. All methods for accessing a critical shared resource must be set to synchronized, otherwise it will not work correctly.

9. Assuming that a method is not known to cause conflict, the wisest way to do this is to avoid using synchronized, which can improve performance.

10. If A/"synchronous" method modifies a variable, and our method uses that variable (which may be read-only), it is best to set our own method to synchronized.

Synchronized cannot inherit, the method of the parent class is synchronized, then the subclass overloaded method does not inherit "synchronization".

12. Thread clogging blocked for several reasons:

(1) thread waiting for some IO operation
(2) The thread attempts to invoke the "synchronous" method of another object, but that object is locked and temporarily unusable.

13. Atomic type operation (atomic), the operation of the primitive type variable (primitive) is an atomic atomic. This means that these operations are thread-safe, but for the most part, we are not using them correctly, to see i = i + 1, i is an int, which is the original type variable:

(1) Read I value from main memory to local memory.
(2) Mount the value from local memory to the working copy of the thread.
(3) Load variable 1.
(4) Add I plus 1.
(5) Give the result to the variable i.
(6) Save I to the thread local working copy.
(7) write back to main memory.

Note that the atomic operation is limited to the reading of steps 1th through 2nd, and to the 6th to 7th step, the value of I is also possible to perform the simultaneous i=i+1 interrupt interrupt (in 4th step).

The double and long variables are non-atomic (non-atomic). The array is an object non-atomic type.

14. For the reasons of article 13, our solution is:


Class XXX extends thread{
I will be frequently modified
private int i;

public synchronized int read () {return i;}

Public synchronized void Update () {i = i + 1;}

..........

}

The volatile variable indicates that it must be consistent with the main memory, which is actually "synchronization of variables", that is, the operation of the volatile variable is atomic, as used before a long or double variable.

16. Using yield () automatically discards the CPU and sometimes improves performance more than sleep.

The difference between sleep () and wait () is that the wait () method is unlocked when called, but where we can use it is only within a synchronized method or block of code.

18. By manufacturing to reduce the synchronization range, as far as possible to achieve code block synchronization, wait (milliseconds) can be in the specified number of milliseconds to exit wait, for wait () need to be notisfy () or Notifyall () to wake up.

19. Construct a method of real-time communication between two threads in a few steps:
(1). Create a PipedWriter and a pipedreader and a pipeline between them;
Pipedreader in = new Pipedreader (new PipedWriter ())
(2). Before the thread that needs to send the message begins, directs the external pipedwriter to its internal writer instance out
(3). Before the thread that needs to accept the message begins, direct the external pipedreader to its internal reader instance in
(4). The degree to which everything is put out can be extracted from in.

In addition to the performance of the synchronized, the biggest drawback is that it will bring deadlock deadlock, only through careful design to prevent deadlocks, and other helpless, which is also a reason why the thread is difficult to tame. Do not use the Stop () suspend () resume () and Destory () methods again

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

22. The main advantage of a thread group is that it uses a single command to complete the operation of the entire thread group. It is seldom necessary to use a thread group.

23. Improve the performance of multithreading in the following ways:

Check all possible blocks and use sleep or yield () and wait () as much as possible.

Extend the time of sleep (in milliseconds) as much as possible;

No more than 100 threads running, not too many;

Different platforms for Linux or Windows and different JVM performance vary 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: info-contact@alibabacloud.com 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.