Use semaphore instead of lock to avoid deadlock on openjdk.