This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Valentin's Question 52  RSS feed

 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I agree with the results, I don't agree with the explanation of question 52.

This scenario doesn't happen:

Imagine the following scenario: t1 starts, it invokes aMethod. i is now 1. t1 releases the lock associated with a and the scheduler decides that it is the turn of t2 to run. t2 invokes aMethod and i's value is now 2.



The code actually creates two Question52 objects, each with its own A object.

The instance methods aMethod() and bMethod() synchronize on the current instance of A. Since ++ and -- are not thread-safe, threads t1 and t2 could easily corrupt static variable i during their combined 20,000,000 executions.
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!