fix bug: write file in wrong order.

The reason of such bug is caused by thread synchronistion.