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

Discussion in 'Java' started by sivasu.india@gmail.com, Apr 9, 2007.

  1. Guest

    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
     
    , Apr 9, 2007
    #1
    1. Advertising

  2. Christian Guest

    Re: How to find a whether a method is used by particular thread ornot?

    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
     
    Christian, Apr 9, 2007
    #2
    1. Advertising

  3. Christian Guest

    Re: How to find a whether a method is used by particular thread ornot?

    Christian schrieb:
    > 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

    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
     
    Christian, Apr 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MJ
    Replies:
    11
    Views:
    1,105
  2. Sabiyur
    Replies:
    2
    Views:
    355
    James Kanze
    Apr 17, 2007
  3. Rupesh
    Replies:
    683
    Views:
    18,675
  4. Pete Emerson
    Replies:
    21
    Views:
    666
  5. Pokkai Dokkai
    Replies:
    3
    Views:
    139
    Jano Svitok
    Jun 11, 2008
Loading...

Share This Page