Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to find a whether a method is used by particular thread or not?

Reply
Thread Tools

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

 
 
sivasu.india@gmail.com
Guest
Posts: n/a
 
      04-09-2007
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

 
Reply With Quote
 
 
 
 
Christian
Guest
Posts: n/a
 
      04-09-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> 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
 
Reply With Quote
 
 
 
 
Christian
Guest
Posts: n/a
 
      04-10-2007
Christian schrieb:
> (E-Mail Removed) schrieb:
>> 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

(E-Mail 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Conditional based on whether or not a module is being used Pete Emerson Python 21 03-16-2010 11:55 PM
allocate particular amount of memory to a ruby thread ? Pokkai Dokkai Ruby 3 06-11-2008 12:09 AM
notifying particular thread to wake up. Rupesh Java 683 11-26-2007 07:52 PM
Determine whether a thread is still running in Perl 5.8.8 with "interpreter threads" Samuel Perl Misc 2 05-10-2007 04:40 PM



Advertisments