• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized blocks and methods  RSS feed

 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have 2 simple questions. I searched the internet for the answers. I found many answers but still not clear.

1. List list=new ArrayList();

synchronized(list){
---------------//somecode//---------
}

What does this mean? I read that it means locking the object. But what does locking the object exactly mean?
So, can the //somecode// be run simultaneously by 2 different objects?

2. I read that,
synchronized public void sum(){
//code//
}

AND

public void sum(){
synchronized(this){
//code//
}
}

mean the same. Then what is the difference between Synchronized methods and blocks?



 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means that only one thread can use this block of code at a time. So if two threads try to enter this code (and they both share a common "list" instance) then one will "win" and get to enter the code (taking the lock on "list") and the other will be "blocked" until the first completes the code block or calls "wait()" (thus releasing the lock).
Code like this is often seen in code where two or more threads trying to do things at once can lead to dangerous (or just plain confusing) situations.

There's nothing really different about synchronized methods or locks, apart from what the lock is held on. With a method the lock is either taken on the Object instance (non-static methods) or the Class instance (static methods). With synchronised block it is taken on whatever you define inside the parentheses.

Here's a very simple example that might help.Imagine 2 threads both working with the same instance of i that has a value of 2. The first decrements i by and check that it is > 0. It is, and so it enters the "if" block. At this point processing changes to the second thread. That enters "unsafe", decrements i and tests it. The test fails, that thread exits cleanly. Then the first thread is brought back and does the division. *bang* DivideByZero. Why? Because just after the first thread checked i >0, the seocnd thread went and made it 0!

This cannot happen with the "safe" method. When the first thread enters "safe" it will get a lock on "i" and even if the second thread enters "safe", it is blocked until the first thread finishes and released the lock.

HTH,

J.
 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jason.

Consider the following code.

class code extends Thread{
synchronized public void run(){
//code//
}
}
public class main{
public static void main(String args[]){
code c1=new code();
code c2=new code();
c1.start();
c2.start();
}
}


So, in this case the run() i.e //code// for both threads c1 and c2 will be running simultaneously. Am I correct?(Because both are different objects).
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the threads / synchronization forum.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Irwin wrote:[code]
...


Although it illustrates the point, synchronizing on an Integer object isn't necessarily a great idea, because of the cache that's maintained for certain values.
The same goes for the other primitive wrappers and, of course, String literals.

 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chandana nannapaneni wrote:Consider the following code.

Please use code tags.
So, in this case the run() i.e //code// for both threads c1 and c2 will be running simultaneously. Am I correct?(Because both are different objects).

Nope, because you have two instances of "code". Each instance can be locked on separately.

You might prefer something like this...As there is only one instance of "MyThing", there is only one instance of "myList" and thus the locking will work as you expect.

I take the point about syncing on a wrapper or a String; but perhaps that's what you want to do. What you sync on will vary depending on the exact design, and this was just a very simple example to get the point across.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Irwin wrote:
chandana nannapaneni wrote:Consider the following code.

Please use code tags.
So, in this case the run() i.e //code// for both threads c1 and c2 will be running simultaneously. Am I correct?(Because both are different objects).

Nope, because you have two instances of "code". Each instance can be locked on separately.


I think there is a bit of confusion here:
In this case the run() methods for both Threads will be running simultaneously - they will not lock each other out (and thus synchronize access between them) because, as you both said, they are different objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!