// A handshake implemented using general semaphores. // // SGM 28/10/99 class handshake1 { private Object local ; semaphore2 semin = new semaphore2(0) ; semaphore2 semout = new semaphore2(0) ; public void send(Object x) { local = x ; // Write message. semin.V() ; // Signal that message is now written. semout.P() ; // Wait here until message has been read. } public Object receive() { semin.P() ; // Wait here until message has been written. semout.V() ; // Signal that message has been read. return(local) ; // Read message. (Note : Java insists that the // `return' must be the last statement in a // returning method, which is why the last two // statements appear to be the wrong way round.) } }