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 / April 2007

Tip: Looking for answers? Try searching our database.

How to find a whether a method is used by particular thread or not?

Thread view: 
sivasu.india@gmail.com - 09 Apr 2007 15:08 GMT
Hi,

      I am not familiar with Thread in java.I have one scenario.Let
me explain conceptually.

suppose i have one method
public synchronised void m1()
{
....
}

and another

public synchronised void m2()
{
......
}

Suppose i have two threads T1,T2.

Now what i want is , if T1 is accessing m1() i don't want T2 to access
m2().Please give me some idea how to do this.Which methods i can use
to identify this?
 I will be very thanlful ,if somebody can exaplain me this with an
example programmatically.

Regards,
Siva
Christian - 09 Apr 2007 15:18 GMT
sivasu.india@gmail.com schrieb:
> Hi,
>
[quoted text clipped - 24 lines]
> Regards,
> Siva

with synchronized keywords thats exactly what you do..

synchronized keyword in a method
is the same as

public void m2(){
  synchronized(this) {

  }
}

since only one thread can hold a monitor of an object only one thread
will be able to access your two methods.

Christian
Christian - 10 Apr 2007 13:57 GMT
Christian schrieb:
> sivasu.india@gmail.com schrieb:
>> Hi,
[quoted text clipped - 41 lines]
>
> Christian
sivasu.in...@gmail.com wrote per mail:
Hey , i think you have not understood the question clearly.
See it T1 is not accessing m1 means ,T2 can access m2.
     if T1 is accessing m1 means,T2 should not access m2.

-----------------------------------
Please don't write me email ... post to the newsgroup

thats nearly what this synchronization here does..

while T1 accesses m1 or m2 , T2 will be unable to access  m1 or m2
if this doesn't fit your needs...

then you should use the java.util.concurrent packet to do some more
appropriate locking...

For example Semaphores can be used for nearly any synchronization you
could imagine..
also with Thread.currentThread()  you can obtain a reference to the
currently executing thread so if here would be a need for checking which
thread is currently accessing a method you can use this..

Christian


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.