0

ZK 5 Desktop cuncurrent page handling

asked 2010-02-24 10:03:39 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

updated 2010-02-24 10:10:59 +0800

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.

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2010-02-25 19:49:07 +0800

iantsai gravatar image iantsai
2755 1

when you try to modify _pages(a Map I think), you need to be careful muti-access, but when you just want to read, you don't have to, because nothing will be changed or incorrect even muti-accesss.

link publish delete flag offensive edit

answered 2010-02-26 02:22:54 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

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 .

link publish delete flag offensive edit

answered 2010-02-26 02:45:04 +0800

tomyeh gravatar image tomyeh
610 1 3
http://blog.zkoss.org ZK Team

updated 2010-02-26 03:18:16 +0800

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.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-02-24 10:03:39 +0800

Seen: 178 times

Last updated: Feb 26 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More