Wednesday, July 31, 2013
In current times, we are always trying to complete our tasks in least possible amount of time. This has given rise to the need for multithreading. In multithreaded application, we have more than one threads running at the same time. We can see that when we are writing some document in Microsoft Word we can write as well as run the spell-checker at the same time. This is done by multithreading. But the real problem arises when a shared resource is being accessed by more than one thread at the same time.
For an example, let us consider that we we are in a situation where a couple Williams and Jennifer, both have an access to the same bank account but have two ATM cards one for each. Now, both of them are trying to withdraw a certain amount from the same account. Now here arises the problem. Suppose, they have now currently $1000 in account and can withdraw a maximum of $300. Before withdrawing, they must check balance and then withdraw. Now Williams checks the balance and waits for withdrawing while in the meantime Jennifer checks balance and sees that she can also withdraw a maximum of $300. Since both have the information that they can withdraw a maximum of $300, it results in "inconsistency" of data. This should be avoided. So we need synchronization.
Synchronization will help in atomic operation. If we take our previous example then Jennifer should not be allowed to access the account until and unless Williams has completed his operation. So, she should be locked from accessing it.
Object locking in Java can be done in two way - intrinsic locks and explicit locks. Intrinsic lock is achieved using the "synchronized" keyword in Java. On using this in a method or block will ensure that all the operations done inside that method or block will be done in a single operation. Explicit locks is done using Lock objects.
We will discuss in detail on intrinsic and explicit locks using Producer-Consumer example in our next posts. Keep in touch with us.
For an example, let us consider that we we are in a situation where a couple Williams and Jennifer, both have an access to the same bank account but have two ATM cards one for each. Now, both of them are trying to withdraw a certain amount from the same account. Now here arises the problem. Suppose, they have now currently $1000 in account and can withdraw a maximum of $300. Before withdrawing, they must check balance and then withdraw. Now Williams checks the balance and waits for withdrawing while in the meantime Jennifer checks balance and sees that she can also withdraw a maximum of $300. Since both have the information that they can withdraw a maximum of $300, it results in "inconsistency" of data. This should be avoided. So we need synchronization.
Synchronization will help in atomic operation. If we take our previous example then Jennifer should not be allowed to access the account until and unless Williams has completed his operation. So, she should be locked from accessing it.
Object locking in Java can be done in two way - intrinsic locks and explicit locks. Intrinsic lock is achieved using the "synchronized" keyword in Java. On using this in a method or block will ensure that all the operations done inside that method or block will be done in a single operation. Explicit locks is done using Lock objects.
We will discuss in detail on intrinsic and explicit locks using Producer-Consumer example in our next posts. Keep in touch with us.
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment