shivdhwaj pandey wrote:how you have prepared for the exam ?
guide me i want to appear for ocpjp 6 in next few days.
arulk pillai wrote:Congrats and keep up the good work
Paul Anilprem wrote:Congratulations!!!
Durgesh Vaishnav wrote:Congratulation
Anayonkar Shivalkar wrote:Well, Prince, to understand this at code level, you can take the banking example from Sierra & Bates' SCJP book.
There, both methods - debit and credit (don't remember the exact names) are non-static and synchronized. You can make 1 method static and see that the code is not thread safe anymore.
e.g. debit is static and credit is non-static. Both are synchronized.
One thread is debiting 5 and other thread is crediting 3. Initial balance is 10.
1) credit thread acquires lock on object, and enters in credit method.
2) debit thread acquires lock on class, and enters in debit method.
3) credit thread reads the balance as 10.
4) debit thread reads the balance as 10.
5) credit thread credits the balance and writes to account. balance is now 13.
6) debit thread has already red the balance as 10, so it will debit 5 and write balance to account. balance is now 5.
In thread safe environment, no matter in which order these operations happen, final balance should always be 8 (i.e. 10 + 3 - 5 or 10 - 5 + 3).
Point here, is not about static or non-static method. It is on which object you are acquiring lock. If both methods are static or non-static, then it is guaranteed that those will try to acquire lock on same object ('this', or classname.class) and the methods will never execute concurrently (on same object of course). But static and non-static combination is like those methods are trying to acquire lock on two different objects, so one method will not wait for another. Static synchronized methods are used to maintain thread-safety of static members of a class.
I hope this helps. Btw, you are welcome to share if you get any example where it is necessary to use both static and non-static synchronized methods
Anayonkar Shivalkar wrote:Hi Prince,
To understand this statement, first, one should understand what is meaning of 'two methods block each other'.
This simply means that - on one object, two methods can never execute simultaneously. One method must wait till other method finishes.
How does this happen? Well, when two methods are object level (non-static) and synchronized, it is same has having a non-synchronized method with all lines of code inside synchronized(this) block.
So, when two threads try to call two synchronized method on same object, due to synchronization, only one thread will have object lock at a time and two threads cannot proceed concurrently.
But what if one method is static synchronized and another method is just synchronized (non-static)? At that time, even if two threads are executing two methods on same object, one thread (executing non-static method) acquires object level lock and proceed. Another thread (executing static method) acquires class level lock (i.e. synchronized(classname.class)) and proceeds (static methods are always class level even if called as obj.method()). And thus, a static synchronized method and a non-static synchronized method will not block each other, ever.
Hope this helps.
Matthew Brown wrote:
Prince Sewani wrote:But the set contains i2 i.e. 46 right??
The set contains 46, which used to be the value of i2. But it isn't any more - you assigned i2 to a different object.
Dennis Deems wrote:
Prince Sewani wrote:But Mathew,
I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?
The set does not contain 47. So the command "set.remove(47)" has no effect. The set is unchanged and its size is still 1.
sanjeev kumar mandal wrote:Hi All,
I need to create a dashboard that will contain the world map along with details of bussiness data for the particular location on the dashboard page.
If i move the cursor from one location to another then the bussiness data must be changed dynamically based on location.
Please suggest me if any third party jar we can use for the display of map and dynamically change the data based on the cursor position or on mouse hover.
Regards
Sanjeev Kumar
Matthew Brown wrote:What would you expect the output to be?
You add two numbers. You add a duplicate, but this is a Set, so it doesn't allow duplicates. You remove one of them. You then try to remove a different value, so that has no effect.