6c3 Subj : Re: Synchronization constructs in Java and their implications To : comp.programming.threads From : Minkoo Seo Date : Mon Sep 26 2005 06:23 am Firstly, I'd like to thank you for your reply. Please, let me leave 'Spinlock' case out. I made a mistake regarding the spin lock construct. Anyway, I still have questions about the other constructs. Following is the code to get a lock using synchronized/Semaphore/Re- entrantLock: // old synchornized keyword public void synchornized_method(int n) { try { startSignal.await(); } catch(InterruptedException ie) { ie.printStackTrace(); } synchronized(this) { System.out.println(dummy + "+" + n + "=" + (dummy+n)); dummy++; } } // lock approach private ReentrantLock lock = new ReentrantLock(); public void lock_method(int n) { try { startSignal.await(); } catch(InterruptedException ie) { ie.printStackTrace(); } lock.lock(); try { System.out.println(dummy + "+" + n + "=" + (dummy+n)); dummy++; } finally { lock.unlock(); } } // semaphore approach private Semaphore s = new Semaphore(1); public void semaphore_method(int n) { try { startSignal.await(); } catch(InterruptedException ie) { ie.printStackTrace(); } try { s.acquire(); System.out.println(dummy + "+" + n + "=" + (dummy+n)); dummy++; } catch(InterruptedException ie) { ie.printStackTrace(); } finally { s.release(); } } To me, all three seem to try to get a lock in a quite similar fashion. But, their performance are different. What's the reason? Could you elaborate? Sincerely, Minkoo Seo. . 0