• Post Reply Bookmark Topic Watch Topic
  • New Topic

without using Synchronized...  RSS feed

 
lakshmi v.samy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I don't want to use Synchronized method, but only one thread can access that method.

Please help me how to do this.

Thanks in advance.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite sure what you mean.

If you mean that you do not want to declare the whole method as synchronised, then there are alternatives. You can use a synchronised block within the method. The advantage of that approach is that you can choose whatever object you like for the monitor, whereas a synchronised method always uses "this".

If you mean that you do not want to use synchronisation at all, then I have to ask why? Synchronisation is the correct Java feature to use, to control multi-threaded access.

If you want to restrict access to a particular method to a single thread at a time, throughout the JVM, you would probably want to synchronise on a static object of some sort. Note that static objects are only unique within a particular ClassLoader. If you have an application with complicated class loading, it may prove very difficult to guarantee only a single thread can access a method, throughout the JVM.
 
lakshmi v.samy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

let me correct my question

Is there other way to prevent next thread to access the resource without using "synchronized" block ?

please help me.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lakshmi v.samy:
Hi,

let me correct my question

Is there other way to prevent next thread to access the resource without using "synchronized" block ?

please help me.


1. Why don't you want to use synchronized? It is extremely fast on modern JVM implementations and I would almost bet you're doing this for the wrong reasons.

2. J2SE 1.5 has a lock which you can use instead of synchronization.



This isn't likely to give you any increase in performance. Why are you avoiding synchronized?
 
Mahadevan Gorti SS
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As rightly said above, unless you have some important/compelling reason to avoid synchronization-lock, there is no need for one to see for other altrenatives.

If you want multiple threads to access a shared resource without sync-lock, you can see Doug Lea's Concurrent HashMap(now in JDK 1.5). You can use similair strategy for your code.

If Still you wanted to go ahead, you can see JDK 1.5 package ' java.util.concurrent.atomic' -- for AtomBoolean, AtomicIntger, AtomicLong for customization for your code(these can be used my mutiple threads to have detectionb mechanism for entring/doing something in a method). But beaware, the methods in those atomic-xlasses might involve tight spinloop(if the other thread takes much time to complete). Also these classes do have native methods -- ofcourse written by Sun , in platform independent way.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lakshmi v.samy:
Hi,

let me correct my question

Is there other way to prevent next thread to access the resource without using "synchronized" block ?

please help me.


Destroy the resource after use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!