• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Confused with Synchronization

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I did not get the description above.Also what is actually Example1 and Example2 doing.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic