Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Threads and synchronization

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't understand the following lines from Thread chapter (page 756) of K & B book.

" ...changeable data stored in a field that is accessed by two different threads. We need to synchronize in order to access that changeable data safely. Fortunately, the same synchronized blocks that allow us to wait() and notify() also provide the required thread safety for our other access to changeable data."

Could someone explain this? How does synchronized(someObject) protect someObject from changes??
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- Multiple threads can be created in a process.
- Same object can be used in those threads.
- Suppose object has information of available tickets, say 20
- threadA checks available tickets
- threadB checks available tickets
- threadA books 18 tickets
- threadB tries to book 5 tickets (as per threadB there are 20 tickets & it can book 5 tickets but threadB is not aware that data has changed)

when one object is used in multiple threads; and you want to do following operation
- Read (because reading data is not changed, so it is always safe)
- Update (write) - you need to make sure when one thread is using that object, no other thread should use that object.
this can be done by locking that object, and locking can be done by synchronizing that object.
(when object is locked, no other thread can use the object until lock is released).

- notify() releases lock on object & other thread becomes eligible to have lock on that object.
- wait(), suspends the processing, until other thread does not notify().

you can use wait(), notify() in synchronization process -- this provide safety for access to changeable data.


Thanks.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heyy thats a great explaination
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Deepika Joshi wrote:-(when object is locked, no other thread can use the object until lock is released).



Deepika,

Thanks for taking the time to write the details of the process. It is a great explanation. However... it doesn't answer my main question
1. An object (say myTicket) is locked by ThreadA,
2. Another thread ThreadB cannot get a lock on myTicket till it is released by ThreadA
3. However, can another thread CHANGE myTicket???

i.e. getting a lock on an object and changing its state are two different things, aren't they?
 
author
Posts: 23882
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nidhi Sar wrote:
Thanks for taking the time to write the details of the process. It is a great explanation. However... it doesn't answer my main question
1. An object (say myTicket) is locked by ThreadA,
2. Another thread ThreadB cannot get a lock on myTicket till it is released by ThreadA
3. However, can another thread CHANGE myTicket???

i.e. getting a lock on an object and changing its state are two different things, aren't they?



Yes. Synchronization and thread safety are two different things. The threads must used synchronization in a cooperative manner. If you don't, it won't work -- as discribed in your example.

Henry
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nidhi Sar wrote:

Deepika Joshi wrote:-(when object is locked, no other thread can use the object until lock is released).



Deepika,

Thanks for taking the time to write the details of the process. It is a great explanation. However... it doesn't answer my main question
1. An object (say myTicket) is locked by ThreadA,
2. Another thread ThreadB cannot get a lock on myTicket till it is released by ThreadA
3. However, can another thread CHANGE myTicket???

i.e. getting a lock on an object and changing its state are two different things, aren't they?



- A process can have multiple threads, & those threads can share someObj; those threads share ownership of someObj; and are eligible to change someObj's state
- Locking is explicit ownership of an object. So if another thread does not have ownership, can not use/update someObj.

Thanks.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Deepika Joshi wrote:
- Locking is explicit ownership of an object. So if another thread does not have ownership, can not use/update someObj.
Thanks.



Deepika, Thanks!!! I did try it out with the following simple logic and that is absolutely the way it works. The first thread CANNOT read or change a shared object, till the second thread releases the object.

 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic