class MyData { private boolean request; private String data; public synchronized void storeMessage(String data) { while (request == true) { try { wait(); } catch (InterruptedException e) { } } request = true; this.data = data; notify(); } public synchronized String retrieveMessage() { while (request == false) { // No data to retrieve try { wait(); } catch (InterruptedException e) { } } request = false; notify(); return data; } }