Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

understanding: synchronized reference

Thread view: 
John_Woo - 09 Aug 2006 12:00 GMT
Hi,

I want some classification on understanding the context of synchronized
reference:

say

LinkedList queue = new LinkedList();

public Object pop() throws InterruptedException {
   synchronized(queue) {
     while (queue.isEmpty()) {
       queue.wait();
     }
     return queue.removeFirst();
   }
 }

public void put(Object o){queue.add(o);}

public void do(){
  pup();
  put(o);
  put(o);
  pup();
 ..
}

case 1:pop never called
case 2: pop called only once
cass 3: pop/put called many times

I'm wondering, in above 3 cases, what happened to queue in terms of
synchronization?
like,
in case 1, is it queue not synchronized?
in case 2, if queue size always > 0 ; queue not synchronized?

when queue is synchronized in pop, did that imply it synchronized
anywhere in same class?

--
Thanks
John
Toronto
Robert Klemme - 09 Aug 2006 12:23 GMT
> Hi,
>
[quoted text clipped - 33 lines]
> in case 1, is it queue not synchronized?
> in case 2, if queue size always > 0 ; queue not synchronized?

The question doesn't really make sense to me.  It reads as if
"synchronizing" does something to the queue content which it does not.
Synchronizing ensures exclusive access to a lock for a single thread.

If queue should be accessed from multiple threads access needs to be
synchronized.  Note, that in Java 1.5 there is are queue classes that
has proper synchronization built in.  See here for example

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

> when queue is synchronized in pop, did that imply it synchronized
> anywhere in same class?

No.  But it must be synchronized in put.  Also, put must invoke
queue.notifyAll() to make pop() wake up.

Kind regards

    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.