Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Atomicity and Safety  RSS feed

 
Scott Selikoff
author
Bartender
Posts: 4081
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's assume there are shared objects or primitives among a group of threads, and there are no synchronization methods nor are these objects known thread-safe classes such as Hashtable/Vector. How much atomicity is there for simple objects or primitives under these circumstances? What kind of thread safey conflicts for reading/writing data can you expect and under what conditions?

Ok that's a loaded question, let me tone it down. I'm not concerned logical errors (such as two threads reading and writinig one after another to get un-desired/illogical results), I mean actual data conflicts such as one thread writing to an ArrayList while another is deleting a member. By simple data types I mean typed arrays, java Collections, wrapper classes, etc, the commonly used ones really.

How atomic are these items handled in terms of being able to produce errors, exceptions, or complete giberrish based on thread violations?

For example, is possible to be halfway through writing an integer, and have another thread read neither the old value or the new value, but some gibberish mixture of the two? I've heard for primitives such as int, this actually doesn't happen, but my point is what are the integrity/atomicity errors for individual operations that we might see? Is it always safe to have two threads reading the same object, or are there even cases where this can conflict?

I understand most people advice to avoid all thread conflicts, but it started me to think, assuming I avoid none and there are no locking issues, what kind of errors might I see other than logical ones.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might see all kinds of strange errors - even references to objects which aren't fully initialized yet.

First, threads can have local copies of variables which are only updated on synchronization boundaries. If you don't synchronize properly, two threads reading the same variable can get different values as results.

Second, statements my be reordered for performance optimization. Those reorderings are guaranteed to lead to the same result *inside one thread*, but without synchronization other threads can see illegal states - such as an object assigned to a reference before its constructor finished.

Anyway, moving to Threads and Synchronization...
 
Henry Wong
author
Sheriff
Posts: 22851
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand most people advice to avoid all thread conflicts, but it started me to think, assuming I avoid none and there are no locking issues, what kind of errors might I see other than logical ones.


It is a very slippery slope. You may be tempted to just live with logical errors, but they quickly lead to other errors.

A logical error on an object could lead to null pointer exceptions, casting exceptions, array out of bounds exceptions, illegal argument exceptions, etc. if the variable is set incorrectly. Even if you doing something like this won't work:



No operation, that is not atomic, is guarranteed to work. And in this case, while both operations may be atomic, the act of the condition, followed by reading it to call the method may not be.

Okay, this case is very contrived, but it is also very simple. Imagine what happens when it gets slightly more complex.

Henry
 
Scott Selikoff
author
Bartender
Posts: 4081
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm interested in non-blocking shared memory most of. For example, if 2 threads read the same array or java collection, neither of which modify the object, will these ever generate errors?

Or, if two threads are writing to the same array but not writing the same elements (thread 1 writes to elements 1-15 and thread 2 writes to elements 16-30), does the access of the array really need to be synchronized? (assuming typed arrays or Collection Arrays with a pre-initialized size).

I'm avoiding object initialization errors, since I agree, those are quite easy to produce, I guess I'm more interested invalid state or data errors that can occur. Do primitives ever need to be synchronized or is updating/reading a primitive guarenteed to be atomic?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are talking about very low level errors then the answer is no. You can not confuse the JVM into crashing. That being said, long and double take 2 write operations to change value, so you can read non-sense from them since I believe a read is allowed in between. I don't think you can corrupt the actual stored value though, but i don't feel up to diving in the spec just now

Fundamentally, the only 'non-logical' error that can happen is the uninitialized object access and the non-sensical value read from long/double/float, and all the issues that come with those.
 
Henry Wong
author
Sheriff
Posts: 22851
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm interested in non-blocking shared memory most of. For example, if 2 threads read the same array or java collection, neither of which modify the object, will these ever generate errors?


If neither thread modifies the array or collection, the array or collection may be effectly considered read-only. Access by multiple threads should be fine.

Or, if two threads are writing to the same array but not writing the same elements (thread 1 writes to elements 1-15 and thread 2 writes to elements 16-30), does the access of the array really need to be synchronized? (assuming typed arrays or Collection Arrays with a pre-initialized size).


For actual arrays, it should be fine. As accessing different members of an array is like accessing different variables. However, this may not be true for collections, as they may be sharing internal variables.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!