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

S

sivasu.india

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
 
C

Christian

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

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
 
C

Christian

Christian said:
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
(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top