add multithreading logic, semapthore logic and optimize old logic and corrected bug

sorry, I only have limited time to do it. I  will try hard!