| Author |
Confused with Synchronization
|
Phillipe Rodrigues
Ranch Hand
Joined: Oct 30, 2007
Posts: 165
|
|
Hi, I am reading the threads chapter for the first time.I tried a sample program on synchronization.Why am I getting different outputs for Example1 and Example 2. Please explain me what actually happens with the two examples.Request a spoon feeding Example1: OutPut Of Example 1: Lucyis going to withdrawl Fredis going to withdrawl Fredcompletes the withdrawl Lucycompletes the withdrawl Fredis going to withdrawl Lucyis going to withdrawl Fredcompletes the withdrawl Lucycompletes the withdrawl Fredis going to withdrawl Lucyis going to withdrawl Lucycompletes the withdrawl Fredcompletes the withdrawl Lucyis going to withdrawl Fredis going to withdrawl Fredcompletes the withdrawl Lucycompletes the withdrawl Fredis going to withdrawl Lucyis going to withdrawl Lucycompletes the withdrawl Fredcompletes the withdrawl Example2: Output of Example2: Lucyis going to withdrawl Lucycompletes the withdrawl Fredis going to withdrawl Fredcompletes the withdrawl Lucyis going to withdrawl Lucycompletes the withdrawl Fredis going to withdrawl Fredcompletes the withdrawl Lucyis going to withdrawl Lucycompletes the withdrawl No enough money in account for withdrawl Mr./Mrs.Fred0 No enough money in account for withdrawl Mr./Mrs.Lucy0 No enough money in account for withdrawl Mr./Mrs.Fred0 No enough money in account for withdrawl Mr./Mrs.Lucy0 No enough money in account for withdrawl Mr./Mrs.Fred0 Request spoon feeding on below. You can make a subset (or indeed all) of the methods for any class object mutually exclusive, so that only one of the methods can execute at any given time. You make methods mutually exclusive by declaring them in the class using the keyword synchronized. Now, only one of the synchronized methods in a class object can execute at any one time. Only when the currently executing synchronized method for an object has ended can another synchronized method start for the same object. The idea here is that each synchronized method has guaranteed exclusive access to the object while it is executing, at least so far as the other synchronized methods for the class object are concerned. The synchronization process makes use of an internal lock that every object has associated with it. The lock is a kind of flag that is set by a process, referred to as locking or a lock action, when a synchronized method starts execution. Each synchronized method for an object checks to see whether the lock has been set by another method. If it has, it will not start execution until the lock has been reset by an unlock action. Thus, only one synchronized method can be executing at one time, because that method will have set the lock that prevents any other synchronized method from starting. Note that there�s no constraint here on simultaneously executing synchronized methods for two different objects of the same class. It�s only concurrent access to any one object that is controlled.
|
Thanks,
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
The example you have shown is trying to demonstrate the object that is used for synchronization when you make a method synchronized. For a non-static synchronized method, the lock is on the instance referred by "this". Any thread can enter a synchronized block of code if and only if it holds the lock on the specified instance(in your case "this"). Additionally, only one thread can hold a lock on an object instance at any moment of time. This means that you can implement mutual execution exclusion for two method using synchronization. However, for different instances of the same class, since the lock instance will be different a mutual execution exclusion is not guranteed. Let me know if the above does not clarify your doubt.
|
apigee, a better way to API!
|
 |
Phillipe Rodrigues
Ranch Hand
Joined: Oct 30, 2007
Posts: 165
|
|
Hi, I did not get the description above.Also what is actually Example1 and Example2 doing.
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
The only difference in Example 1 and Example 2 (atleast looking at it casually) is: All threads in Example 1 uses different AccountDanger objects(i.e. in turn different AccountBalance objects) On the other hand, all threads in Example 2 uses same AccountDanger object(i.e. in turn same AccountBalance objects) Since, the makeWithdrawl method in AccountDanger is synchronized, so, at any moment of time only one thread, using the same AccountDanger, can execute this method. In example 1, since every thread has its own AccountDanger/AccountBalance instance, all threads(Fred, Lucy ..) are able to successfully withdraw amount of 50. In example 2, since all threads share the same AccountDanger/AccountBalance object, the amount that one can withdraw depends on whether the thread can get a lock and enter the method makeWithdrawl(). The exact amount withdrawn by each thread can not be predicted and one of the Thread will get an "Not enough money" message. The examples are trying to demonstrate that the lock used for synchronization in a synchronized method is the object referred by the variable "this".
|
 |
 |
|
|
subject: Confused with Synchronization
|
|
|