I have been writing code since 1980. In 1999, I learned Java and after discovering IntelliJ the love was complete. XML and XSLT was a revelation in 2000, too bad the standards committees have done so much damage since. Contributed to the Flying Saucer open source XML and CSS rendered and published an open source XSLT generator, weffo. Currently employed by Google. Torbjörn is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Thread-safe state handling

03.19.2008
| 3821 views |
  • submit to reddit
Handling mutable state and side-effects in multi-threaded code can be a challenge, perhaps more of a challenge than you might think. In my latest blog entry I explore some issues and options, especially how the State pattern can help.
Published at DZone with permission of Torbjörn Gannholm, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Artur Biesiadowski replied on Thu, 2008/03/20 - 11:41am

Have you done any tests if your state pattern is faster then plain synchronization? It will probably depend on number of concurrent threads modifying/reading the state. It should be faster in at least some of the cases, otherwise plain synchronize or java.util.Concurrent lock is probably better solution (a lot simplier and no ugly things like 'Off extends Lamp').

Torbjörn Gannholm replied on Fri, 2008/03/21 - 4:46am in response to: Artur Biesiadowski

You're right on Artur, I haven't tested it for speed, so I don't know those trade-offs. I "discovered" the pattern in some real code I was writing and the point was that it made the functionality a lot easier to analyze and prove correct.
I suspect the speed aspect varies from case to case and depending on usage patterns.

Ash Mughal replied on Thu, 2012/01/26 - 2:12am

Thread safety is a programming concept applicable in the context of programs that are multi threaded. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures

advanced java

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.