Multithreading design highlights-JSP tutorial, object-oriented/Design

Source: Internet
Author: User

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 ).

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.