my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchornized inside run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchornized inside run" Watch "Synchornized inside run" New topic

Synchornized inside run

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Continuing with my Thread vs Runnable doubts, in the following code:

Here, which object is locked for the synchronized. Is it Thread object or Runnable?

The output prints:

this is the object of thread1.SynchInRunnable@119c082

which doesn't tell me a whole lot
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
i just called the holdsLock method in ur code.

The output is true.

If u type cast this(current object) to Thread it will give compiler error.
so it holds the runnable object lock.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Here's another way of knowing that "this" is a Runnable, not a Thread:

The "this" object always refers to an instance of the enclosing class, which is SynchInRunnable in this case. Since SynchInRunnable is a Runnable, not a Thread, "this" must be a Runnable.

To find out what class an object is an instance of, you can do this:

The output is:
this is an instance of SynchInRunnable
[ April 14, 2005: Message edited by: Joe Sanowitz ]

SCJA 1.0 (98%), SCJP 1.4 (98%)
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Getting a little off topic, but if you really want to test whether something is a subclass or implementer of something else, you can run reflection code like this:

[ April 14, 2005: Message edited by: Joe Sanowitz ]
I agree. Here's the link:
subject: Synchornized inside run
It's not a secret anymore!