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 synchronized threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized threads" Watch "synchronized threads" New topic
Author

synchronized threads

prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
<html>
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
}
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}
</html>
ans:i.v is guaranteed always to be 0 or 1.
j.v is guaranteed always to be 0 or 1.

i just want to know,
suppose there is one object,it has three threads then is it possible that one thread is acceesing method a(),second thread acessing b(),& third thread accssing c()method simultaneously?
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

I think the question is: "Can different threads simultaneously access different synchronized 'things' within a class?".

The answer is yes and no. Since 'things' could be methods or blocks of code.

If a thread is accessing a method, which is marked as synchronized, the thread will need the lock of the object that is represented by the class the method is defined in. Ergo, no other thread would be able to access any other synchronized methods in the same class.

BUT...if you have blocks of code synchronised on different objects, then different threads could access those blocks if they could obtain the pertinent lock.

Cheers,

Si.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

You can try the example below and see that once one of threads enters one of the synchronized methods in the ZZ class, the other thread can't enter the other method until it has finished...




Cheers,

Si.
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
...and in this example you have synchronized blocks..

If you run it as it is since both methods are synchronized on lock1, different threads cannot run the methods concurrently.

BUT, if you change one of the methods to be synchronized on lock2, then they can.




Cheers,

Si.
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
hi simon,
very nice explanation u have given.
thanks a lot.
prajakta
Yingtao Jiang
Greenhorn

Joined: Nov 05, 2002
Posts: 13
Hi, Simon,

I think you make a mistake here.

If a thread is accessing a method, which is marked as synchronized, the thread will need the lock of the object that is represented by the class the method is defined in. Ergo, no other thread would be able to access any other synchronized methods in the same class.


This is true only for static method. For instance method, the lock is on the receiver object. As long as the reciever object is different, two threads can concurrently enter synchronized method that operate on different object of the same class.

once one of threads enters one of the synchronized methods in the ZZ class, the other thread can't enter the other method until it has finished...
Please see the modified example of yours below.


From the running result, you can see that, thread-0 and thread-1 can concurrently run aMethod() and bMethod(). As sleep() method do not give up lock, this example demonstrate that the two thread keep two different lock, which belongs to two instance of the same class.

Hope I made my point clear.


SCJP1.4<br />SCJD1.4 (preparing)
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Ah yes...you are right of course. I don't know why I was merely illustrating the static case.

If the the lock is based on an object which is an instance of a class...then I think the methods could be accessed by different threads if they could obtain the lock on the appropriate object.

Cheers,

Si.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized threads