The Artima Developer Community
Sponsored Link

Java Answers Forum
What is it doing ?!

2 replies on 1 page. Most recent reply: Aug 1, 2003 6:42 AM by DJ

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
DJ

Posts: 2
Nickname: cipherchap
Registered: Aug, 2003

What is it doing ?! Posted: Aug 1, 2003 6:13 AM
Reply to this message Reply
Advertisement
Could somebody please explain why shouldn't the argument of the synchronized block be an instant of the SyncTest class?

class SyncTest extends Thread
{    
   public void run()    
   {        
       synchronized(SyncTest.class)   
       {             
          ......            
       }    
   }    
 
   public static void main(String[] args)    
   {        
       new SyncTest().start();        
       new SyncTest().start();    
   }
} 


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: What is it doing ?! Posted: Aug 1, 2003 6:18 AM
Reply to this message Reply
You are not passing an INSTANCE of the SyncTest class to the synchronized block. You are passing the class object of the SyncTest class. This is typically used when synchronizing on static methods. It should read:

synchronized(this) {
}

DJ

Posts: 2
Nickname: cipherchap
Registered: Aug, 2003

Re: What is it doing ?! Posted: Aug 1, 2003 6:42 AM
Reply to this message Reply
The code is indeed an extract from an example. Since I am new to synchronization, I would really like to know the mechanism behind this sample, especially to how should we decide on which object be taken as an argument for the synchronized block ? Say for instance, why should the class object be used instead of the instance object in this case ? Appreciate if a detail elaboration could be provided. Thanx.

Flat View: This topic has 2 replies on 1 page
Topic: Applets Previous Topic   Next Topic Topic: swing question help this newbie

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use