refactor suggestion
・thread locking with lock statement
・simplify conditions
・code prettify