Thursday, June 3, 2010

Have you aware of the problems using toUpperCase and toLowerCase method's in Java String API?

The toUpperCase() and toLowerCase() method's in String API's internally use the Locales. And hence these functions won't work with some of the locales (like turkish e.t.c).

For example consider the following toUpperCase() method code, it won't return the uppercase of the specified string value. To avoid that, you need to explicitly set English locale to the toUpperCase() and toLowerCase() method's. Here turkish language has a problems with charecter "i", so it won't return the expected value.

public class TestAgain

{

public static void main (String[] args) {
Locale locale = new Locale("tr" , "TR");

Locale.setDefault(locale);
String val = "DeleteAllVersions";
val = val.toUpperCase(Locale.getDefault());

System.out.println("val:" + val);

}
}

Iterator Vs Enumerator

1. By using iterator we can remove the value but in ennumerator we can't.

2. Enumeration is twice as fast as Iterator and uses very less memory.

3. Iterator is much safer as compared to Enumeration b’ coz it always denies other threads to modify the collection object which is being iterated by it. Whenever a second thread tries for that Iterator will throw a ConcurrentModificationException. Iterators that do this are known as fail-fast iterators as they fail quickly and cleanly

Iterator Vs ListIterator

Iterator : Enables you to cycle through a collection,obtaining or removing elements

ListIterator :It extends Iterator allow bidirectional traversal of list & the modification of element

Websphere - java.io.IOException: Async IO operation failed

1. The problem only happens when finishBufferResponse request is processed asynhronously and it is possible to prevent the problem by forcing all such requests to be handled synchronously. This can be done by setting the webcontainer custom property:

com.ibm.ws.webcontainer.channelwritetype=sync


2. The above errors can cause the Http connections to be terminated and can cause session timeouts. To prevent this, you may need to set this custom property

Difference between ConcurrentHashMap and Collections.synchronizedMap

1. ConcurrentHashMap - It allows concurrent modification of the Map from several threads without the need to block them.
Collections.synchronizedMap(map) creates a blocking Map which will degrade performance, albeit ensure consistency (if used properly).

2. Use the second option if you need to ensure data consistency, and each thread needs to have an up-to-date view of the map. Use the first if performance is critical, and each thread only inserts data to the map, with reads happening less frequently.

3. ConcurrentHashMap is optimized for concurrent access.

4. Hashtable and ConcurrentHashMap do not allow null keys or null values.

Collections.synchronizedMap(Map) synchronizes all operations (get, put, size, etc).

ConcurrentHashMap supports full concurrency of retrievals, and adjustable expected concurrency for updates.

How to make read only objects - Collections

The java.util.Collections class contains the following methods to make objects read only.

1. unmodifiableList(List list)

Returns an unmodifiable view of the specified list.

2. unmodifiableMap(Map m)

Returns an unmodifiable view of the specified map.

3. unmodifiableSet(Set s)
Returns an unmodifiable view of the specified set.

ConcurrentHashMap

It allows concurrent modification of the Map from several threads without the need to block them.

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations.

However, even though all operations are thread-safe, retrieval operations do not entail locking. ConcurrentHashMap is optimized for concurrent access.

surprised deleteOnExit() method on java.io.File

The commons-fileupload jar (commons-fileupload-1.0.jar) got a problem of leaking memory on usage of certain functions (shown below)

parseRequest(HttpServletRequest req) method of class org.apache.commons.fileupload.FileUploadBase
includes call to getOutputStream() and subsequently to getTempFile() of class org.apache.commons.fileupload.DefaultFileItem. This in turn calls deleteOnExit on java.io.File which is reported to have memory leaks.

This deleteOnExit() will cause memory leaks in your application. This issue was resolved using the latest jar file commons-fileupload-1.2.1.

Difference between Fail fast and Fail safe Iterators

Iterators can be either fail fast or fail safe. Depending on the underlying implementation of the Iterator (Fail Fast), a ConcurrentModificationException is thrown if the Collection is modified while Iterating over the data structure.

The fail safe iterator (CopyOnWriteArrayList ) makes a copy of the internal array data structure and uses it to iterate over the elements. This prevents any concurrent modification exceptions from being thrown if the underlying data structure changes

The CopyOnWriteArrayList does not fail when the internal structure changes, but where as ArrayList does. The CopyOnWriteArrayList is A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.