It's not a secret anymore!*
The moose likes Java in General and the fly likes Problem with Synchronized methods.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with Synchronized methods.. " Watch "Problem with Synchronized methods.. " New topic
Author

Problem with Synchronized methods..

Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

We actually have a concept that a Synchornized method dosent allow more than one thread to enter in its code..so why did this happen??

[Added code tags - see UseCodeTags for details]

/*
outputs:
8-1 8-2 9-1 9-2
8-1 9-1 9-2 8-2

*/
why the thread with Id=9 entered a synchronized method although the thread with Id=8 was running in the hit( ) method..
please can any one help me out with a good reason..??


OCPJP
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

The thing you need to ask is, which objects are they synchronized on? You have two Runnables, and each one is synchronized on itself. So there's no conflict. If you want to see something different you need to synchronize them both on the same object.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Matthew Brown wrote:The thing you need to ask is, which objects are they synchronized on? You have two Runnables, and each one is synchronized on itself. So there's no conflict. If you want to see something different you need to synchronize them both on the same object.


I got somewhat that you are trying to explain but not fully. so like do i have to use a synchronized Block for these program?? which common object do i use to synchronize the method? m confused with this synchronized method..
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Ishan Pandya wrote:
Matthew Brown wrote:The thing you need to ask is, which objects are they synchronized on? You have two Runnables, and each one is synchronized on itself. So there's no conflict. If you want to see something different you need to synchronize them both on the same object.


I got somewhat that you are trying to explain but not fully. so like do i have to use a synchronized Block for these program?? which common object do i use to synchronize the method? m confused with this synchronized method..



Now is it the thing what you were saying??
class ThreadTestID implements Runnable
{
public synchronized void hit(long id)
{
for(int i=1;i<3;i++){
System.out.print(id+"-"+i+" ");
}
}
public static void main(String []args)
{
ThreadTestID t1=new ThreadTestID();
new Thread(t1).start();
new Thread(t1).start();
}
public void run()
{hit(Thread.currentThread().getId());}
}
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

OK, let's start with this. Do you realise that this:

is shorthand for using a synchronized block and is exactly equivalent to this:

In other words, it obtains a lock on a specific object, which in this case is this. So each thread is locking a different object.

So, we want to change it so that they both lock on the same object. There are a couple of options here. One is to add a static variable referencing an object, and then synchronize on that. Because it's static, both objects will use the same one. Or you can just use the Class object itself:


In this particular case there's another thing you could do. There's nothing stopping you making the hit method static. And a static synchronized method uses the class object, just as I showed above. But that's only possible because the hit method doesn't use any instance variables or methods. Whereas the synchronized block approach is always available.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Ishan Pandya wrote:Now is it the thing what you were saying??


Yes, that's another alternative. Now you only have one Runnable, so both threads obtain a lock on the same object.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Matthew Brown wrote:OK, let's start with this. Do you realise that this:

is shorthand for using a synchronized block and is exactly equivalent to this:

In other words, it obtains a lock on a specific object, which in this case is this. So each thread is locking a different object.

So, we want to change it so that they both lock on the same object. There are a couple of options here. One is to add a static variable referencing an object, and then synchronize on that. Because it's static, both objects will use the same one. Or you can just use the Class object itself:


In this particular case there's another thing you could do. There's nothing stopping you making the hit method static. And a static synchronized method uses the class object, just as I showed above. But that's only possible because the hit method doesn't use any instance variables or methods. Whereas the synchronized block approach is always available.



Yes sir now i definitely got it. was confused between locks.. i understood that class lock in the synchronized block..
thankyou so much for the support..
and one more question..
Threads share a common copy of the instance variable of a class and has its own private copy of the local variables.. is this statement true??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Ishan Pandya wrote:and one more question..
Threads share a common copy of the instance variable of a class and has its own private copy of the local variables.. is this statement true??

If you mean what I think you mean, then yes.

In your second example, each thread is using the same Runnable. So if it had any instance variables (which it doesn't at the moment) then it would be the same for each. But local variables and method parameters are local to the specific call that's being made - your two threads each have completely separate values for id and i.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Matthew Brown wrote:
Ishan Pandya wrote:and one more question..
Threads share a common copy of the instance variable of a class and has its own private copy of the local variables.. is this statement true??

If you mean what I think you mean, then yes.

In your second example, each thread is using the same Runnable. So if it had any instance variables (which it doesn't at the moment) then it would be the same for each. But local variables and method parameters are local to the specific call that's being made - your two threads each have completely separate values for id and i.


yes got it..that is what i was actually asking.. i am preparing for ocjp 6.. thanks again..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Synchronized methods..
 
Similar Threads
Deadlock threads in Java
Doubt in Threads Synchronization
Question 13 chapter 9 of k&B
basic Thread run synch question
Thread doubt