wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronization 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 "synchronization" Watch "synchronization" New topic
Author

synchronization

Asanka Vithanage
Ranch Hand

Joined: Oct 24, 2008
Posts: 59
following code gives AABB as out put .but since public void run is synchronized i am expecting ABAB as out put.because synchronized method can
access by only one thread at a time .Am i correct or plese explain the theory and reason for this output



SCJP 1.5 (94%)
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Asanka,

You are correct that even when a thread sleeps it doesn't give up the locks it has acquired. The problem in this case is that each thread can execute the run() method concurrently, since there are two locks at play (one for each thread.) When you have a synchronized method you acquire the lock of the object used to invoke the method, and in this case we have two objects (each of the threads,) so concurrent execution is not a problem.

Ruben


All code in my posts, unless a source is explicitly mentioned, is my own.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ya Ruben is absolutely correct here.

Here you have two different objects lock that will be taken by two thread.
If one thread object would have been shared between two threads than your output would be right.
That will happen in this case:


Tg a=new Tg();
Thread t1=new Thread(a);
t1.start();
Thread t2=new Thread(a);
t2.start();


Here thread t1 and t2 will execute same object a. So they will always try to acquire lock on one instance of Tg object a.


SCJP 6
Asanka Vithanage
Ranch Hand

Joined: Oct 24, 2008
Posts: 59
pleae explain more .............

i thought when we call a synchronized method it is locked that method and when finished the work it release the lock.no other thread can execute it ,
it looks i had understoood the theory wrong
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Asanka it is a simple concept. Let me try to explain it to you. Let's take a simple example first to make the basics clear



Here you are creating two separate instances of MyThread and calling start on them. So the output will be AB or BA. I think this would be clear. Now let's modify the code a bit-



Now the output can be ABAB (it can vary though). Now if I synchronize the run method. That would make no difference. This is because m1 and m2 are separate instances of MyThread. So they will not block each other. Let's take another example for this.



Now the output will be abab. This is because both the threads t1 and t2 share the same instance of MyRunnable. So they both can call method one at a time. But if both the threads had different instances of MyRunnable, then they could be able to call method at the same time. This is reflected in the code below



Now you can see that there are two separate instances of MyRunnable. So the output will be aabb. I hope you will get it this time as it is very difficult to explain


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Tg a=new Tg();
a.start();
Tg b=new Tg();
b.start();


Theory says that every object has one lock.
So when you made

Tg a=new Tg();

then a will have one lock say LockForA.

when you did

Tg b=new Tg();

then again different lock for b say LockForB.

When you call a.start(), this thread will try to get a's lock means LockForA.

But when you call b.start, this thread will try to get b's lock not a's lock means LockForB.

So both thread got the lock for their corresponding object, they will execute the run() method simultaneously.


Now another case.

Tg a=new Tg();
Thread t1=new Thread(a);
t1.start();
Thread t2=new Thread(a);
t2.start();


Here whey you call Tg a=new Tg(); a new lock for object a will be created say LockForA.
when you call t1.start(); it will try to get the object a's lock that is LockForA. as Thread t1=new Thread(a);, means t1 is running run() method of object a that is synchronized.

when you call t2.start(); it will again try to get the object a's lock that is LockForA, that is already acquired by t1 thread, so it will wait for the lock. When t1 thread will execute the object a's run() method, it will release the lock, so t2 thread will get the lock and start executing the run() method of object a.

If you have any doubt ask...
Asanka Vithanage
Ranch Hand

Joined: Oct 24, 2008
Posts: 59
i got the point .
thank you ankit and punit for you,r greate explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization