public void run() { while (true) { synchronized (this.queue) { if (this.queue.isEmpty()) { try { System.out.println(name + " waiting for task"); queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // method should be synchronized this.task = queue.getNextTask(); this.length = task.length(); } doTask(); } }