Programming Practice Note {Java thread concurrent processing Webservice} (RPM)

Source: Internet
Author: User
Tags volatile

Http://www.cnblogs.com/mingzi/archive/2009/03/09/1406694.html

1, three ways to ensure thread safety:
A, do not access shared variables across threads
b, so that the shared variable is the final type
C, synchronize the operation of the shared variable

2, it is easier to design a class as thread-safe from the beginning than to repair it later.

3, write multithreaded programs, first of all to ensure that it is correct, and then consider the performance.

4, stateless or read-only objects are always thread-safe.

5, do not expose a shared variable to a multithreaded environment (no synchronization or immutability protection)

6, lazy loading in a multithreaded environment requires synchronous protection, because lazy loading causes objects to be instantiated repeatedly

7, it is often unsafe to operate on variable-type variables of the volatile Declaration (volatile can only guarantee visibility and cannot guarantee atomicity).
See the volatile principles and techniques for a discussion of dirty data issues.

8, when a thread requests to acquire its own lock (nested use of the same lock), we call the lock as a reentrant lock.
In the jdk1.5 and bundle, a Java implementation-reentrantlock that can be re-entered is provided.

9, each shared variable should be protected by a uniquely determined lock.
Create the same number of reentrantlock as the variables so that they are responsible for thread safety for each variable.

10, while reducing the range of synchronization blocks, can improve system performance.
However, in the case of guaranteed atomicity, atomic operations cannot be decomposed into multiple synchronized blocks.

11, in the absence of synchronization, the command execution order of the compiler and processor runtime may be completely unexpected.
The reason is that the compiler or processor reordered the instructions (reordering) in order to optimize its execution efficiency.

12, when a thread reads a variable without synchronization, it may get an expiration value, but at least it can see the
A true value set by the thread at that time. Rather than the value of a vacuum. This safety guarantee, known as the minimum security (Out-of-thin-air safety)

When developing concurrent applications, this insecure approach is sometimes used to significantly increase the throughput and performance of the system.
However, the operation of the numerical value is still rejected.

The volatile variable can only guarantee visibility and cannot guarantee atomicity.
See volatile principles and techniques

14, some time-consuming network operations or IO, to ensure that the execution, do not occupy the lock.

15, the Publish (publish) object, which means that it can be used by code outside the current scope. (Reference delivery)
Object escaping (Escape), which refers to an object that is published when it is not yet ready.

Principle: In order to prevent escaping, the object must be fully constructed before it can be released (the best solution is to use synchronization)

This keyword refers to an object escaping
Example: In a constructor, the thread is opened and its own object this is passed into the thread, causing the reference to pass.
At this point, when the constructor has not finished executing, the object escapes.

16, if necessary, use the threadlocal variable to ensure thread closure (closed threads tend to be more secure, but to some extent will result in performance loss)
Examples of closed objects are common in practical use, such as the hibernate opensessioninview mechanism and the connection mechanism of JDBC.

17, a single immutable object is often thread-safe (complex immutable objects need to ensure that their internal member variables are not mutable)
A good multithreaded programming habit is to declare all domains as final unless they are mutable

18, to ensure that the sharing of the variable is safe to publish
A, initialize the object through a static initializer (JLS 12.4.2 narration, the JVM guarantees that the static initialization variables are synchronous)
b, declare the object to be volatile or use atomicreference
C, ensure that the object is immutable
D, the reference or variable operation is protected by a lock

19, the basic elements that should be included in designing a thread-safe class:
A, determine what is mutable shared variables
b, determine which variables are immutable
c, specify a policy that manages the state of concurrent access objects

20, the data is encapsulated inside the object, and the data access is guaranteed to be atomic.
It is recommended to use the volatile JavaBean model or to construct synchronous getter,setter.

21, thread-restrictive makes it easier to construct thread-safe classes, because when the state of a class is limited, it is not necessary to examine the complete program when it parses its thread security.

22, write concurrent programs, need more full comments, more complete document description.

23, using the Java Monitor mode is better than using the monitor lock of your own object when you need to subdivide the allocation of locks.
The former is more flexible.

Object target = new Object ();
External objects are used here as monitors, not this
Synchronized (target) {
Todo
}

For Java monitor pattern, the Reentrantlock implementation is actually much easier to program concurrently.
Functional, but also more powerful.

24, in the design of concurrent programs, in order to ensure scalability and performance in the premise of compromise, the priority is to delegate shared variables to the thread-safe class.
It controls the global concurrency access.

25, an iterator using an ordinary synchronization container (Vector, Hashtable) requires an external lock to ensure its atomicity.
The reason is that the iterator produced by the normal synchronization container is non-thread-safe.

26, in concurrent programming, when container support is required, the use of JDK concurrency container is preferred
(Concurrenthashmap, Concurrentlinkedqueue, copyonwritearraylist ...).

Concurrenthashmap, Copyonwritearraylist
Iterators for concurrent containers, and a full range of size (), isEmpty () show weak consistency.
They can only indicate a data state at the time of the container. The changes and modifications after the container cannot be fully responded to.

28, use bounded queue, block all read and write operations when the queue is full or empty. (A good solution for production-consumption)
Blockqueue implementation has linkedblockingqueue and Arrayblockingqueue, the former is a linked list, variable operation is frequently preferred, the latter is an array, read operations are frequently preferred.
Priorityblockingqueue is a priority-ordered blocking queue that can sort all placed elements (implementing the Comparator interface)

29, when a method, can throw interruptedexception, means that this method is a blocking method, if it is interrupted, will prematurely end the blocking state.
When you call a blocking method, it also means that itself is called a blocking method because you have to wait for the blocking method to return.

If the blocking method throws an interrupt exception, what we need to do is to throw it to the top, unless it is currently the level at which the exception needs to be caught.
If the current method cannot throw interruptedexception, you can use the Thread.currentThread.interrupt () method to interrupt manually.
===================================================
WebServices: Find *.wsdl file in Project Engineering--"WebServices" Test with WebServices exploer

Programming Practice Note {Java thread concurrent processing Webservice} (RPM)

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.