File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in threads 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 "Doubt in threads" Watch "Doubt in threads" New topic
Author

Doubt in threads

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Can anybody explain me the below statements with a example?

1. Threads calling non-static synchronized methods in the same class will block only each other if they are invoked using the same instance. That's because they each lock on "this" instance and if they are called using two different instances, they get two locks, which do not interfere with each other.


2. Threads calling static synchronized methods in the same class will always block each other they all lock on the same class instance.


3.A static synchronized method and a non-static synchronized method will not block each other, ever. The static method locks on a class instance while the non-static method locks on this instance- these actions do not interfere with each other at all.


4.For synchronized blocks, you have to look exactly what object has been used for locking (Whats inside the parentheses after the word synchronized?) Threads that synchronize on the same object will block each other. Threads that synchronize on different objects will not.

Source: k & b


Thanks All
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

These statements all come from the same idea: Each class has a class lock, and each instance (object) has its own instance lock.

For example, if you have...

A a1 = new A();
A a2 = new A();
A a3 = new A();

...then there are 4 locks. Each instance (a1, a2, and a3) has its own instance lock, and the class itself (A) has a class lock. Note that there is only one class lock, regardless of how many instances might be created.

A thread calling a synchronized instance (non-static) method must obtain the instance lock. A thread calling a synchronized class (static) method must obtain the class lock. A thread calling a synchronized block must obtain the lock dictated by what's in the parentheses after "synchronized".

Does this help?
[ May 02, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Marc,
I did not get the below statement

Threads calling non-static synchronized methods in the same class will block only each other if they are invoked using the same instance.

I have one more doubt for the synchronized block what lock will be obtained is it class lock or instance lock.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Hi Marc,
I did not get the below statement

Threads calling non-static synchronized methods in the same class will block only each other if they are invoked using the same instance.

I have one more doubt for the synchronized block what lock will be obtained is it class lock or instance lock.


If there is only one object, there will be only one monitor. If all the thread s are using the same monitor (object lock), they will block each other, because at a time only one thread can acquire the lock and enter the synchronized code. If one thread that is acquiring the lock releases the lock, then only other threads will to acquire that lock will be able to jump to get that lock, but only one of them will win the lock to enter the sync code.

Let me give you some examples of class lock:



cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi chandra,

In the below statement what do you exactly mean by threads will block each other

If there is only one object, there will be only one monitor. If all the thread s are using the same monitor (object lock), they will block each other
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by nik arora:
...I have one more doubt for the synchronized block what lock will be obtained is it class lock or instance lock.

It could be either, depending on what's in the parentheses following "synchronized". If this references an object, then the object (instance) lock is required. If is a class literal, then the class lock is required.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Feel the code below:



Output:
Jocky wants to get the toy to play with
Rocky wants to get the toy to play with
Ninjo wants to get the toy to play with
Jocky has got the toy
Jocky is playing with toy
Jocky is playing with toy
Jocky is playing with toy
Rocky has got the toy
Rocky is playing with toy
Rocky is playing with toy
Rocky is playing with toy
Ninjo has got the toy
Ninjo is playing with toy
Ninjo is playing with toy
Ninjo is playing with toy


See how one thread blocks to others!
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi marc,
Can you give me a example?
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi chandra,

This is the statement from k & b

Threads calling non-static synchronized methods in the same class will block only each other if they are invoked using the same instance. That's because they each lock on "this" instance and if they are called using two different instances, they get two locks, which do not interfere with each other

Below is your code

public static void main(String... args) { Toy toy = new Toy();
Thread t1 = new Thread(toy,"Rocky"); Thread t2 = new Thread(toy,"Jocky"); Thread t3 = new Thread(toy,"Ninjo");
t1.start();
t2.start();
t3.start();}

Look at the below statement

Threads calling non-static synchronized methods in the same class will block only each other if they are invoked using the same instance.

My doubt is in your code you have created three instance that is t1,t2,t3 but the statement tells that threads will block each other on the same instance. Hey am i right or wrong. Correct me if i am wrong.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by nik arora:
Hi marc, Can you give me a example?

In the code below, a new Thread (t) obtains the class lock to execute the synchronized static method "syncMeth()" which just sleeps for 5 seconds. The main thread (separate from t) then calls the instance method blockMeth, which contains a synchronized block. As written below, that block is synchronized on the class literal Sync.class, so the class lock must be obtained. Therefore, the main thread is blocked until t finishes with syncMeth and releases the lock.

On the other hand, if you change the synchronized block to use this instead of Sync.class, then the main thread will need to obtain the instance lock instead of the class lock, and the main thread will not be blocked by t having the class lock.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

public static void main(String... args) { Toy toy = new Toy();
Thread t1 = new Thread(toy,"Rocky"); Thread t2 = new Thread(toy,"Jocky"); Thread t3 = new Thread(toy,"Ninjo");
t1.start();
t2.start();
t3.start(); }

Look at the below statement

Threads calling non-static synchronized methods in the same class will block only each other if they are invoked using the same instance.

My doubt is in your code you have created three instance that is t1,t2,t3 but the statement tells that threads will block each other on the same instance. Hey am i right or wrong. Correct me if i am wrong.


Monitor(class instance) is a vehicle and threads t1, t2, t3 are drivers.
Threads will only block each other when they are invoked on the same instance (vehicle).

And finally, there is only one Toy instance and three users Rocky, Jocky and Ninjo, all willing to play with the only one instance of Toy available in my code, so they block to each other. Once a thread acquires the toy (lock),
it runs inside the sync block code (play with the toy). All other threads stay out from the sync block and one of them can only enter when the previous one leaves the toy (stops playing with that).
[ May 02, 2007: Message edited by: Chandra Bhatt ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The code below illustrates what happens when 2 threads use different instances when calling a synchronized instance method.

To see what happens when 2 threads use the same instance, comment out lines 1 and 2, and un-comment lines 3 and 4.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Marc for your neat code and fantastic description

for you
[ May 02, 2007: Message edited by: Chandra Bhatt ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Marc,
I am confused with your code and chandra's code. Can you summarize everything and give a example please?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by nik arora:
...I am confused with your code and chandra's code. Can you summarize everything and give a example please?

I think that's what we were trying to do: Summarize with examples. What points are you questioning?
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Marc and Chandra,
Thanks for all that explanation. I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in threads