-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I do not know, if this is a bug or not. The ZK 5 DesktopImpl has methods:
public Collection getPages() { //No synchronized is required because it cannot be access concurrently return _pages.values(); } public void addPage(Page page) { //We have to synchronize it due to getPage allows concurrent access synchronized (_pages) { ... } } public void removePage(Page page) { synchronized (_pages) { ..} }
I do not understand why sometimes _pages are accessed without synchronisation and sometimes synchronised.
Note: removePage is in DesktopImpl, but not in Desktop.
Yes, it is a Map. If the getPages() is accessed concurrently, then it may be accessed just when some page is being added or removed. In such case, the Map may be temporarily in inconsistent state (and getPages() may throw some exception). It requires deep understanding of the Map implementation (I think the HashMap is used), to be sure that this code is safe. I think you code is not safe for HashMap, see Javadoc: http://java.sun.com/javase/6/docs/api/java/util/HashMap.html :
If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.)
And it suggests to use Collections.synchronizedMap .
No, ZK is made to be thread safe while application/component developers don't need to worry about threading. It is safe and no synchronization required to access any component/resource belonging to a desktop as long as the code is executed in an event listener or an activated server push (i.e., when Execution is available).
It also means you cannot access the resource of a desktop in other situations, such as a working thread (non-server push) or an event listener for another desktop. First it jeopardizes the threading assumption. Second, it is pointless -- non of the change will be sent back to client (remember it is HTTP).
For this case, why synchronized is used in getPages()? Well, it is for implementation purpose. Some ZK engine code might access these methods without really locking the execution (performance reason). Since the kind of 'dirty' accesses are limited, we don't protect _pages with a synchronized map.
Asked: 2010-02-24 10:03:39 +0800
Seen: 178 times
Last updated: Feb 26 '10