The Artima Developer Community
Sponsored Link

Synchronization at menthod level



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Synchronization at menthod level

Posted by Dawit Alemayehu on 17 Jul 1998, 7:23 PM


I have enjoyed you Design Technique article ever since they
appeared six months ago. This month's article just like the
others explain the concept precisely and clearly in a manner
that can be conceptualized by beginning or veteran programmers
alike. Having said that I have one question regarding this month's

Let say we chose to synchronize our accessor and modifier methods
as follows (straightforward way):

// synchronized modifier method
public synchronized setColor (int R, int G, int B) {

// synchronized accessor method
public synchronized int[] getColor () {

The blue thread steps through the setColor method is preempted
to sleep before it finishes. The red thread then starts and tries
to access the getColor method. Is it going to be blocked ? In
other words, does putting the synchronize keyword at the method
level automatically locks it from access by a second thread, once
another synchronized method is in use and locked by another one ?

Thank you.

Dawit A.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us