JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Object-level-locking. Show all posts
Showing posts with label Object-level-locking. Show all posts

Object level locking vs Class level locking in java

8:01 AM
Synchronization is ability to restrict access to shared resource to only one thread. When two or more threads need access to shared resource, there has to be some mechanism such that shared resource will be used by only one thread. The process by which we can achieve it is called Synchronization. Why do you need Synchronization? Let’s understand this with the help of example. Let’s say you want to count number of request you got for a particular URL. If you get two requests at the same time, then count may be inconsistent. Without Synchronization: 1 2 3 4 5 6 7 8 9 10 11 12 13 14   package org . arpit . java2blog ;   public class RequestCounter { private int count ; public int incrementCount ( ) {    count ++ ;    return count ; } }   For example: Thread T1 sees count as 20 and increment it to 21. At the same time, thread t2 also sees count as 20 and increment it to

.