File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in synchronized....

 
sridhar Ranganathan
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is synchronized block? How it will work?Give me simple ex.
Why synchronized is not working in this below pgm.

public class test18 extends Thread
{

String setString="";
test18(String set)
{
setString=set;
start();

}
public void run()
{

try{
callLogic();


}catch(InterruptedException op)
{
System.out.println("Exception : "+op);
}
}
private synchronized void callLogic() throws InterruptedException
{
for(int i=0;i<100;i++)
{

System.out.print(setString);
Thread.sleep(100);

}


}

public static void main(String[] args)
{
test18 ob1=new test18("A");
test18 ob2=new test18("B");
test18 ob3=new test18("C");


}

}

output :--->

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB


Synchronized means :- It will allow only one thread at time right? That mean output will come
AAAAAAAAAAAAAAAAAAAA 100times and BBBBBBBBBBBBBBBBB 100 times and CCCCCCCCCC times. What is the problem here,

Thanks & Regards,
Sridhar.R
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridhar,

Synchronization is to manage access to data from multiple threads.
Some classic example is web hit counter, where you have 1 object that keep track of the number of visits but you have multiple threads of request/servlet which tries to increment the same instance of the counter object.

The example that you have is not working because each test has their own instance of data that they manipulate. So what you see is behavior of standard thread-based operation where you have multiple threads doing their own thing that are being managed by JVM where each thread has its own chance to run.

Do some Google search, there are plenty of tutorials on concurrent programming which give clear example what synchronization do.
[ May 08, 2008: Message edited by: Wirianto Djunaidi ]
 
arulk pillai
Author
Ranch Hand
Posts: 3380
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in method level (coarse grained lock � can affect performance adversely) or block level of code (fine grained lock). Often using a lock on a method level is too coarse. Why lock up a piece of code that does not access any shared resources by locking up an entire method. Since each object has a lock, dummy objects can be created to implement block level synchronization. The block level is more efficient because it does not lock the whole method.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arulk pillai:
Often using a lock on a method level is too coarse.


In general a fine grained lock is good to have when the lock contention is high.
However, when the lock contention is less then it is better to keep it simple.
Fine grained locks make the code a little cluttered/hard to decide the sync boundaries and has scope for bugs that are hard to debug.
It is like worrying about saving resource which no one else is actually using.
[ May 09, 2008: Message edited by: Nitesh Kant ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic