Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Thursday, 5 March 2015

Synchronization in Java

 Unknown     07:14     Concurrency, Java, Thread     No comments   

What is thread synchronization?
Suppose we have a multi-threaded application i.e. our application have more than one thread.Since we know multiple threads can share an object.Suppose we have an object which is shared by multiple threads because of this unexpected results can occur unless access to shared object is managed properly.For example:We have two threads which are going to update that shared object what will happen if one thread is updating that object and another thread is in process of updating that thread and another thread is going to read that data.Then what will happen,Which data the third thread will read?
Old data or first thread's data or second thread's data.
What will happen if third thread was supposed to read that old data but has read the new data either of first or second thread.So data is not correct.
The above problem can be solved by giving only one thread access to shared thread on time exclusive basis and at the same time if another thread wants to access that shared object then it has to wait until the thread with that exclusive lock has finished its operation.So this  operation is called as Thread Synchronization.By synchronizing threads in this manner ensures that only one thread is using that shared object and other threads are waiting to access that object.This is called as Mutual Exclusion.
We need to synchronize only mutable data, there is no need to synchronize the immutable data.

How to perform synchronization?
Now we know basics of synchronization.Our next question is how to perform this synchronization in Java.
In Java,There is a concept of monitor.Each object has a monitor and a monitor lock(also known as intrinsic lock). This monitor is used to perform synchronization.Monitor ensures that the monitor lock is held by at most one thread at a time and this ensures mutual exclusion.If a thread want to access a shared object then it has to acquire the lock so that thread will be allowed to access that objects data.Other threads attempting to perform the operation which requires the same lock will be blocked until the first thread releases the lock and after that other thread can acquire the lock to perform operation.
The code which will be executed by the threads will be put in the synchronized block.The code inside the synchronized block is said to be guarded by the monitor lock.So thread has to acquire the lock in order to use that guarded block.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates