my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes synchronization not working properly 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 » Java » Threads and Synchronization
Bookmark "synchronization not working properly" Watch "synchronization not working properly" New topic
Author

synchronization not working properly

rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
Hi,
I dont understand why the synchronization is not working properly within this code.
Though I have one synchronized method, I am sure that two threads are aceeesing the code simultaneously.

The output says so.



The output is unexpected.

You can see from the output that deposite method is not synchronized.
Help me with the reason why this is happening.



Rakesh Kadulkar
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
Using the synchronized keyword with a method causes the method to be synchronized on the current object. Since the objects are different (ref2 != ref3), no synchronization is happening.

You could make "bal" an Integer object, and then synchronize on that:


Ping & DNS - my free Android networking tools app
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Rakesh,

Actually, the synchronized function is working perfectly in your code. It is just that it does not do what you expect.

You create two instances of Account1. That means that each of the instances will prevent multiple thread from entering their deposite functions. But that does not mean that they will prevent the static bal variable from being modified by two threads. To fix this you need to protect the bal variable, not the functions.

The following is a quick way of doing this:



Cheers


[url]http://FrancoisNadeau.com[/url]
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
You synchronized a method. That prevents access on that method by multiple threads on the SAME INSTANCE of the object. That is why both calls to deposit are executed in the same time, because the calls are made on different objects.

If you want to synchronize something for all instances, you should synchronize an object which is common for all instances. As a sample this.getClass(). Try the following code:
Or transform bal into Integer type (can't synchronize on primitive types) and synchronize on that:

Hope it helps!


http://javasign.blogspot.com/
rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
Thanks for the reply.
The problem is solved but one thing how can i create two different threads of the same object.

For example.

class MyThread extends Thread
{
public void run()
{}

public static void main(String args[])
{
MyThread ref1 = new MyThread();
ref1.start();
ref1.start(); // Is this valid
}
}

Abhishek Jadhav
Greenhorn

Joined: Jun 24, 2009
Posts: 3
No this will not create two threads instead it will throw an exception.
But you can try this:


This wil create two different threads with the same target run method of MyThread object.

-Abhishek
Maruthi Janardhan
Greenhorn

Joined: Aug 04, 2009
Posts: 16
Aurelian Tutuianu wrote:
Or transform bal into Integer type (can't synchronize on primitive types) and synchronize on that:



This seems to be a common pitfall. Integers are immutable objects. If we synchronize on them and then change the Integer by assigning a primitive (Autoboxing: bal = 300), a new Integer object would be created and hence the synchronization will no more be on the same object.


-MJ
www.leviossa.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization not working properly