A basic question

Discussion in 'Java' started by jack, Jul 1, 2003.

  1. jack

    jack Guest

    I have one basic java question, but seems may not have answer.

    How do I check the monitor of one object?

    For example

    synchronized(myobj) {

    mystatmenet

    }



    If I can't go to mystatemnet, means I can't get the lock of myobj, but
    before I call synchronized(myobj), can I check whether it is locked,
    and which thread is locking it?
    jack, Jul 1, 2003
    #1
    1. Advertising

  2. jack () wrote:
    : For example
    : synchronized(myobj) {
    : mystatmenet
    : }
    : If I can't go to mystatemnet, means I can't get the lock of myobj, but
    : before I call synchronized(myobj), can I check whether it is locked,
    : and which thread is locking it?

    Not from within java, if you care to use a bit of jni and jvmpi you can
    do it, but then you might get a "its locked" returned and before you have
    decided what to do the lock might have been dropped.

    Why do you need it? Why can't you just synchronize and be happy?

    /robo
    Robert Olofsson, Jul 2, 2003
    #2
    1. Advertising

  3. On 1 Jul 2003 10:11:04 -0700, jack wrote:
    > I have one basic java question, but seems may not have answer.
    >
    > How do I check the monitor of one object?
    >
    > For example
    >
    > synchronized(myobj) {
    >
    > mystatmenet
    >
    > }
    >
    > If I can't go to mystatemnet, means I can't get the lock of myobj, but
    > before I call synchronized(myobj), can I check whether it is locked,
    > and which thread is locking it?


    No. The monitor can only be used to deny or grant access to a section
    of code. But you can use that to build more complex types of
    synchronization if you need it. Here is an extremely simple, untested
    example:

    public class Mutex {
    private boolean taken;
    Thread owner;

    public Mutex() {
    taken = false;
    owner = null;
    }

    public void synchronized lock() {
    while (taken) {
    wait();
    }
    taken = true;
    owner = Thread.currentThread();
    }

    public void synchronized unlock() {
    if (owner == Thread.currentThread()) {
    taken = false;
    owner = null;
    notify();
    }
    }

    public boolean synchronized tryLock() {
    if (taken) {
    return false;
    }
    else {
    taken = true;
    owner = Thread.currentThread();
    return true;
    }
    }

    public boolean synchronized isLocked() {
    return taken;
    }

    public Thread synchronized owner() {
    return owner;
    }
    }

    To get the functionality you want, create an instance of Mutex and
    synchronize uting its methods instead. If you need to test before
    taking a lock (in order to avoid blocking, or something) then you
    can't test and lock separately, use tryLock() for that.

    For more and better examples, see:
    http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

    /gordon

    --
    [ do not send me private copies of your followups ]
    g o r d o n . b e a t o n @ e r i c s s o n . c o m
    Gordon Beaton, Jul 2, 2003
    #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. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    432
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    568
    Eric Smith
    Jul 8, 2005
  3. Engineer
    Replies:
    6
    Views:
    606
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    418
  5. Ark Khasin

    A basic question question about volatile use

    Ark Khasin, Jul 31, 2008, in forum: C Programming
    Replies:
    12
    Views:
    353
    Ark Khasin
    Jul 31, 2008
Loading...

Share This Page