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

What is synchronized ?

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys, Can anybody explain me what is synchronized keyword and how to use in a running thread?
explain me with an example.
thanks.,
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read this part of the Java Tutorials? It explains what synchronization does and why you need it, with examples.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Have you read this part of the Java Tutorials? It explains what synchronization does and why you need it, with examples.


Steve , When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block until the first thread is done with the object.

we can do this by wait() , join() , yield() isn't it?
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usman Shahir wrote:Steve , When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block until the first thread is done with the object.

we can do this by wait() , join() , yield() isn't it?

No. wait() will wait for a signal, not an open lock, and must be in a synchronized block. join() waits for a thread to complete, which is far different than waiting for a block of code to complete. And yield() offers up some of the current Thread's processor time if other Threads need to use it. These are all different than what synchronized blocks are for.

The fact that synchronization makes you wait on the lock is almost a necessary side effect. You don't use it because you want threads to block, you accept that other threads will block to protect the data the code is using.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic