To execute the method, a thread must acquire the lock of the object that owns the method. class MyData { private boolean request; private String data; public synchronized void storeMessage(String data) { request = true; this.data = data; } public synchronized String retrieveMessage() { request = false; return data; } }