• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Do I need to have synchronized keyword in the code ???

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

Consider the following code

[Added code tags - see UseCodeTags for details]


Output

A about to wait.
B about to wait.
A interrupted.
A terminating.
B interrupted.
B terminating.


my question is that do i need to have synchronized keyword here is the run method..It looks like I need that but I want to understand why??

I am calling run method on two different objects..so every object will have it's own method copy..so they should not block here...and every thread get's it's local thread copy..

Any suggestions??
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is your call to wait(). If you look at the Javadocs for java.lang.Object#wait(), you'll see:

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Throws:
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.



The thread owns an object's monitor if it's in code synchronized on that object. So if you took out the synchronized keyword you'd get an IllegalMonitorStateException.
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking if you need the keyword "synchronized"? or do you need something to synchronize (mutex) your code?

In the beginning, the only way to write thread safe code was to use the keyword "synchronized"

Java 5 implemented a number of new classes and methods for synchronization, so you can use things like ReadWriteLock and achieve the same effect. So the keyword is not the only way to do it, but you must write thread safe code if you use threads.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic