Java Distributed Application Learning notes 04JDK and package collection Summary-later

Source: Internet
Author: User

 

Alas ~ This blog can only display some of the content, and some of the content is truncated ....

 

4. Set concurrency

The underlying implementation of CopyOnWriteArraySet is similar to that of CopyOnWriteArrayList. When adding elements, You need to determine the uniqueness of the object. If the object array already contains repeated elements, no additional processing is performed. I will not go into details here.

5. Queue concurrency

The concurrency class of the queue is java. util. concurrent. ArrayBlockingQueue. We can guess from the class name that the underlying layer still uses arrays. This ArrayBlockingQueue is inherited from the original java. util. AbstractQueue. Therefore, many methods already exist in the parent class. It only adds a lock pair mechanism to key methods for inbound and outbound queue operations.

The source code of the inbound queue element operation is as follows:

Java code

Public boolean offer (E e, long timeout, TimeUnit unit)

Throws InterruptedException {

 

If (e = null) throw new NullPointerException ();

G nanos = unit. toNanos (timeout );

Final ReentrantLock lock = this. lock;

Lock. lockInterruptibly ();

Try {

For (;;){

If (count! = Items. length ){

Insert (e );

Return true;

}

If (nanos <= 0)

Return false;

Try {

Nanos = notFull. awaitNanos (nanos );

} Catch (InterruptedException ie ){

NotFull. signal (); // propagate to non-interrupted thread

Throw ie;

}

}

} Finally {

Lock. unlock ();

}

}

 

When the array is not full, when the insert operation is executed, if the array is full, wait, in the unit of nanoseconds. If it times out or is awakened, Judge again whether the array is full. If the thread is interrupted, an exception is thrown. The out-of-queue method is similar to the in-queue method.

6. Atomic class of AtomicXXXX

The concurrent package also provides the Atomic series class that supports Atomic operations. Let's take a representative class, the AtomicInteger class. When we use counter operations, we generally want to avoid thread security issues, lock the method. With the atomic series class under the concurrent package, we can use it directly. The key code snippets are as follows:

 

 

Java code

Public static int getSum (){

Return sum. incrementAndGet ();

}

 

The key to the underlying fragment of the auto-increment method is this sentence.

Java code

If (compareAndSet (current, next ))

Return next;

 

The method is as follows:

Java code

Public final boolean compareAndSet (int exact CT, int update ){

Urn unsafe. compareAndSwapInt (this, valueOffset, exact CT, update );

}

 

Unsafe. compareAndSwapInt calls the local native method to directly deal with the underlying hardware, that is, the CPU. If you are interested, you can decompile sun's code. The underlying layer checks whether the value on the address in the memory is the current value. If it is next, it repeats until the current value is found. This is similar to the optimistic lock of Hibernate. Some other atomic classes, such as AtomicBoolean and AtomicLong, will not be repeated here. Their usage and underlying principles are similar.

7. Summary and reflection

The collection classes for concurrent packet sending are summarized here. The performance of the read elements in the set is not compared this time. In actual applications, the read ratio of high concurrency changes to the set elements (add, remove, replace) it is more common, but the code is very simple, so I will not give it. If you are interested, you can experiment with these classes on your own. As for reflection, it should be the performance of these concurrent package resources, whether it occupies a lot of memory space, added to a high concurrency environment, and the hardware environment cannot be allocated to hardware resources that are very tolerant to the application system, in this case, whether or not to play with the game (such as cloud computing virtualization, a single real machine may start multiple virtual machines as an extension of the real machine ). We can use a tool to test jconsole for monitoring this problem, or there may be a series of problems with the user's application code. We still need to analyze the specific problems, in general, to achieve thread security for concurrent packet sending, and the time efficiency is similar to that for non-concurrent packet sending in the general environment, the memory resources consumed are more than before, this cannot be avoided. The world is material and everything needs to be done at a cost, but it is not worth the cost compared with the benefits.

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.