use monitor and condition variable to reduce code