Go deep into the list traversal problem and analyze the implementation of the observer mode in spring and tomcat.

Source: Internet
Author: User

You can traverse a list in either of the following ways:

List list = new arraylist ();
For (INT I = 0; I <list. Size (); I ++ )...{
//...
}

Another method is to use the iterator interface, as shown below:

Iterator ite = List. iterator ();
While (ITE. hasnext ())...{//...
}

So what are the differences between the two methods? The answer is that there is no difference in the sequential execution program, but there is a difference in the parallel program. If another thread modifies the list in the traversal process, the execution result of the For Loop is uncertain, and the iterator will quickly fail (fast-fail) and throw concurrentmodificationexception. Therefore, iterator is used to traverse collection objects first.

Sometimes we need to ensure that the set is not modified during the traversal process, so we need to synchronize the set.

Synchronized (list )...{
Iterator ite = List. iterator ();
While (ITE. hasnext ())...{
//...
}
}

In the preceding method, you need to completely lock the accessed list object during the traversal process. If the traversal process takes a long time, it may cause performance degradation and deadlock. Therefore, you can use the temporary COPY method to traverse the private temporary set after the copy.

Object [] snapshot;
Synchronized (list )...{
Snapshot = new object [list. Size ()];
For (INT I = 0; I <snapshot. length; ++ I)
Snapshot [I] = list. Get (I );
}
For (INT I = 0; I <snapshot. length; ++ I )...{
//...
}

The following is an example of the observer mode. A target object has multiple observers. When the target State changes, it sends a notification to each observer. The following is a method of the tomcinerbase class in tomcat5:

Public void firecontainerevent (string type, object data )...{

If (listeners. Size () <1)
Return;
Containerevent event = new containerevent (this, type, data );
Containerlistener list [] = new containerlistener [0];
Synchronized (listeners )...{
List = (containerlistener []) listeners. toarray (list );
}
For (INT I = 0; I <list. length; I ++)
(Containerlistener) list [I]). containerevent (event );

}

It can be seen that it adopts the temporary COPY method.
The following is a method of the applicationeventmulticasterimpl class in Spring:

Public void onapplicationevent (applicationevent e )...{
Iterator I = eventlisteners. iterator ();
While (I. hasnext ())...{
Applicationlistener L = (applicationlistener) I. Next ();
L. onapplicationevent (E );
}
}

It can be seen that spring adopts the non-synchronous iterator method.

I think spring is not properly written. This involves the Exception Handling Methods for server-side applications and client applications. Client Applications can use non-synchronous iterator. When an exception occurs, the system prompts the operator to stop or retry the operation. However, for server applications, you must ensure that no exceptions occur during traversal. As a general component management framework, spring cannot be assumed to be used.

Note: The collection class in the Java. util. Concurrent package in Java 5 provides another form of weakly-consistent iterator. It is different from the traditional Fast-fail iterator. They promise to be thread safe, but don't promise whether you will see any updates made to the collection since the iterator was constructed (e.g ., you might see an added element, or you might not ).

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.