This blog has moved to Medium

Subscribe via email


Java Puzzle – spot the bad code

What’s the most important fault in the following java code (thanks to Roman for both writing and finding the bug :))?

public class Worker extends Runnable {
...
 
    @Override
    public void run() {
        while(true){
            synchronized (emailMessages){
                try {
                    while(emailMessages.isEmpty()){
                        emailMessages.wait();
                    }
                    mappings.saveMultiple(emailMessages);
 
                }catch (InterruptedException e) {
 
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(e);
 
                } finally{
                    emailMessages.clear();
                }
            }
        }
    }
}

One Comment

  1. ripper234:

    Well, nobody replied yet, so I can reveal the answer – the most serious problem with this code is very simple – it doesn’t catch exceptions properly! It’s hard to see at first because of the business logic and multi-threading, but this code can terminate without logging any exceptions, which makes debugging a bitch.