Essentials of Java Multi-threaded programming

Source: Internet
Author: User
Tags garbage collection resource sleep thread volatile

1. Multiple threads have main memory and working memory, in the JVM, there is a main memory, dedicated to all threads sharing data, and each thread has his own private working memory, main memory and working memory decibel in the JVM stack and heap area.

2. The state of the thread has ' Ready ', ' Running ', ' sleeping ', ' Blocked ', and ' waiting ' in several states, ' Ready ' which indicates that the thread is waiting for the CPU to be allocated for the time it is allowed to run.

3. Thread-run order does not run in the order in which we created them, the order of CPU processing threads is indeterminate, and if so, it must be done manually, using the SetPriority () method to set the priority.

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

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

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

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

8. All methods of accessing a critical shared resource must be set to synchronized, otherwise they will not work properly.

9. Assuming that a known method does not create a conflict, it is advisable not to use synchronized to improve performance.

10. If a "sync" method modifies a variable, and our method uses this variable (possibly read-only), it is best to set this method to synchronized.

Synchronized cannot inherit, the parent class method is synchronized, then "Sync" is not inherited in the subclass overloaded method.

12. There are several causes of thread blockage blocked:

(1) The thread is waiting for some IO operation

(2) The thread tried to invoke the "sync" method of another object, but the object was locked and temporarily unavailable.

13. Atomic operation (Atomic), the operation of the original type variable (primitive) is the atomic of the atom type. This means that these operations are thread safe, but in most cases we do not use them correctly to see i = i + 1, i is int, and the original variable:

(1) reads the I value from the main memory to the local memory.

(2) Load the value from local memory into the thread working copy.

(3) Load variable 1.

(4) I plus 1.

(5) Give the result to the variable i.

(6) Save I to the thread local working copy.

(7) write back the main memory.

Note that the atomic operation is limited to the reading of steps 1th through 2nd, as well as the 6th to 7th step, and that the value of I may be interrupted at the same time by I=i+1 multithreading (in the 4th step).

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

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

class xxx extends Thread{
  //i会被经常修改
  private int i;
  public synchronized int read(){ return i;}
  public synchronized void update(){ i = i + 1;}
  ..........
  }

The volatile variable, the volatile variable, guarantees that it must be consistent with the main memory, which is actually a "synchronization of variables," meaning that the operation for the volatile variable is atomic, such as 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 invoked, but where we can use it is only in a synchronized method or block of code.

18. By manufacturing to reduce the synchronization range, as much as possible to achieve code block synchronization, wait (milliseconds) can be specified in the number of milliseconds to exit wait; for wait () need to be notisfy () or notifyall () kick awake.

19. The method of constructing real-time communication between two threads is divided into several steps:

(1). Create a PipedWriter and a pipedreader and a conduit between them;

Pipedreader in = new Pipedreader (new PipedWriter ())

(2). Before the thread that needs to send the message starts, the external pipedwriter is directed to its internal writer instance out

(3). Before the thread that needs to receive the information starts, the external Pipedreader is directed to its internal reader instance in

(4). All the things that are put out of this way can be extracted from in.

In addition to the decline in performance, the biggest drawback of synchronized is that it leads to deadlock deadlock, and that only by carefully designed to prevent deadlocks, there are other things that are difficult to tame. Do not use the Stop () suspend () resume () and Destory () method

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

22. The main advantage of the thread group is that it can complete the operation of the entire thread group using a single command. There is little need to use a thread group.

23. Improve the performance of multithreading from the following aspects:

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

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

No more than 100 threads running, not too many;

Different platforms Linux or Windows and different JVM performance vary widely.

Related Article

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.