• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is synchronized ?

 
Ranch Hand
Posts: 80
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.,
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic