Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A basic question

Reply
Thread Tools

A basic question

 
 
jack
Guest
Posts: n/a
 
      07-01-2003
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?
 
Reply With Quote
 
 
 
 
Robert Olofsson
Guest
Posts: n/a
 
      07-02-2003
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
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      07-02-2003
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/...ent/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
 
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
TurboTax Basic vs. Taxcut Basic? Sharp Dressed Man Computer Support 1 01-12-2009 12:52 PM
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57