I'm a bit confused about the use of "for" in the above statement.( i read the line from the oracle tutorials ) I suppose a thread has its run() method , inside which there is some code. That code might call a method of an object... and by this point of time , my mind has locked on to firing "method of an object" but i keep reading "for an object". What does "for" signify here ? Can someone please help me get untangled from this grammatical nuance ?

EDIT: This should be in the java forum!! I messed up and posted it here ! i'm hoping the mods could move this there.. apologies for my stupidity.

Recommended Answers

All 3 Replies

Method of an object seems to imply a method defined for that object type
Method for an object seems to imply the object is the invocation target when the method is called.

What is a Thread ? I understand it to be an instance of a class that implements Runable , and thus has a run() method , which is executed when the jvm works the thread ? Inside that run() method there will be some lines of code , let's suppose at any particular line , a synchronized method bar() of object foo is invoked , Now , when we say

First, it is not possible for two invocations of synchronized methods on the same object to interleave

i suppose it means another thread cannot call foo.bar() untill the 1st thread has finished. Thus , the object being mentioned is foo , and the invocation being mentioned is of the bar() method ?

and then , when its said :

When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

it means that : If one thread is executing the bar() method for foo , foo's intrinsic lock is currently owned by that particular thread , and all the other threads are waiting in blocked state for the lock held by the running thread to be made free , ie , the current thread to stop executing ?

What you say is basically right, exceopt the very first para. A thread is an execution environment - part of a process. It's not an instance of a class. It is represented by an instance of the Thread class. It is started by Thread's start method which starts the thread then calls a run() method which can either be in an instance of something that implements Runnable, or a subclass of Thread that overrides run().
Apart from that, yes. If one thread gains the intrinsic lock for a synchronised method, or the explicit lock for a syncronised block, any other thread will have to wait for the first to release the lock before it can gain the lock itself and thus excute any synchronised code using the same lock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.