Producer-consumer multithreading problem solved in Java