• 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's the use of synchronize() here?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was trying to trap the system with the following code:

There is a cinema with 2 seats. There are two guys suffering from narcolepsy who come in and try to reserve. Both attempt to reserve the first seat and then snooze for 50 ms, Then wake up and try to reserve the second seat.

I have the following run() method




So I have the following classes: Seat and Cinema




and the threads are declared in the main





I get the following output sometimes:

fredo reserved seat 0
seat 0 is reserved so you can't have it mr. michael
michael reserved seat 1
fredo reserved seat 1


Which means I did succeed in messing the cashier up for giving seat number 1 to both.

But what I don't understand is how is this solved by synchronizing the occupied method?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But what I don't understand is how is this solved by synchronizing the occupied method?




Well, in order to understand why synchronization will work, you need to first understand why it is currently not working.

To start, when both people call the reserveSeat() method, this could lead to the simultaneous calls to the occupy() method. Since the arrays have already been set prior, there is no chance that they will be calling the wrong method.

Now, in the occupy() method, it is a bit more complex. The method checks to see if it is occupied, and if it isn't, it will occupy it. This unfortuantely, has a race condition. What if both threads call the method, and both checks to see if it is occupied -- and determine that it isn't. Or basically, if the first thread determines that it is not occupied, but before it can occupy it, the second thread determines that it is not occupied -- then, both threads will eventually occupy it and return true.

This is an issue because the if-then instructions are not atomic. It is possible for another thread to determine that a seat is not occupied, in between the time the first thread determines that the seat is not occupied, and occupy it. Which brings us to the topic of synchronization....

Synchronizing the occupy() method, will prevent the race condition from happening in the method. Only one thread will be able to call occupy() at a time, so there is no chance that another thread can get in-between the determination and the occupation.

Hope this helps,
Henry
 
Ali Khalfan
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, it does help,

Thanks,
reply
    Bookmark Topic Watch Topic
  • New Topic