Code prettify and thread locking with lock statement