• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

without using Synchronized...

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic