Friday, August 25, 2006

why need to synchonize on an object before calling wait method

A very nice answer I found online:

Just to reinforce things. The reason to call wait() is because you are waiting for a particular state to be achieved - which will be signified by someone calling notify or notifyAll. That state has to be, by definition, shared mutable state (it is shared between you and the notifier thread and it must be mutable or you will be waiting a very very very long time). As you know access to shared mutable state must always be performed while holding a suitable lock (AtomicXXX and lock-free concurrent collections not-withstanding). So you have to hold the lock to check the state you are going to wait() upon. The wait() atomically releases the lock and suspends the thread. The notification thread then acquires the lock, modifies the state and calls notify()/notifyAll(). At that point the wait()ing thread is removed from the wait-set and is now waiting to reacquire the lock that the notifying thread may not have yet released. When the waiting thread returns from wait() it owns the lock again and should then re-check the state to see if the new state is what it is waiting for. If it is then, generally, while still holding the lock the thread performs some action that relied on the object being in the state it waited for.

Note that wait() must always be called in a loop testing the condition being waited upon. Spurious wakeups are permitted and do occur.

To me, I think the reason of requiring the synchonization is to ensure the atomicity of "checking the status and wait" operations. So that no other thread can change the status in the middle.

Tuesday, August 08, 2006

xsd:all tag

I am using xsd schema to validate xml files and using xsd:all tag to group elements. Based on w3 documents, the default minOccurs of an element in xsd:all should be zero. (meaning it can just not show up without explicitly set its minOccurs to zero). But I found this is not true for any xsd validators I tested (XML spy, xerces SAXParser). I have to set the minOccurs to zero for each element if I want it to be optional.
What makes thing worse is the misleading parsing error message when I am using xerces packages in java. I started testing adding minOccurs attributes to some elements to see if this will solve the problem. However the error message does not change, i.e. the element I just added 'minOccurs=0' still seems mandatory based on the error message. I guess this is a bug in the xerces package. Only utill I fixed all the elements, the error message is gone. Whereas in XML spy, it's much better. Whenever I added minOccurs for an element, I can see it is no longer required when I try to validate the xml. This is how I figured the default value of minOccurs for elements in xsd:all group is one.

Tuesday, August 01, 2006

Install PHP5 with Apache2 and mysql on windows

Today I installed php5 on my working desktop to edit my baby's website:
Here is my experience:
Windows + Apache 2 + PHP 5 installation (This only applies to Apache 2.0.x, for 2.2.x, the dll doesn't work)

1. Install Apache.
2. Unzip PHP to a directory, add it to path and PHP_HOME
3. Copy php\php.ini-recommended to windows\php.ini
4. Edit php.ini, set doc_root to apache htdocs directory
5. Edit php.ini, set extension_dir, uncomment extension=php_bz2.dll.
6. Install php as Apache module: Add the following lines to "httpd.conf" file under apache conf directory.
LoadModule php5_module "php/php5apache2.dll"
AddType application/x-httpd-php .php
7. Add test page phpinfo.php, restart Apache to see.

Everything is easy, except one tricky point. The above steps work only for Apache version 2.0.x, it will not work for latest 2.2.x. Obviously the php dll files are not fresh for 2.2.x.

After that, I try to configure php working with existing mysql database:
Still everything is clear, two steps involved: uncomment php_mysql.dll ext in php.ini and expose "libmysql.dll" file in "path". However this will not work, I am getting the error of: "PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysql.dll' - The specified module could not be found.\r\n in Unknown on line 0"

After google for a while I found an excellent solution:

You have to copy "libmysql.dll" file to "windows\system32" directory. Yes, I know, it is already in the Path, you still have to do it. I believe it is related to windows security issue set by MS. Seems this is a very common problem, but the answer is not so obvious from php or mysql website. I update a wiki to post the solution: