commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <j...@apache.org>
Subject [jira] Commented: (MATH-505) TestUtils is thread-hostile
Date Tue, 01 Feb 2011 02:34:29 GMT

    [ https://issues.apache.org/jira/browse/MATH-505?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12989013#comment-12989013
] 

Sebb commented on MATH-505:
---------------------------

By thread-hostile, I mean that it is not possible in general for two different threads to
use the class safely.
If one thread changes any of the static fields, there is no way of knowing how the methods
called by the other thread will behave. This is partly because the values are not safely published
currently, but even if they were, the threads don't know what settings will be used as they
can be changed at any time by another thread.

In general, any class which relies on mutable static state for its behaviour is thread-hostile.
The shared state cannot simultaneously satisfy two threads needing different behaviour.

I think the only safe way for two threads to use the class as it stands is if they both synchronize
on the class.
This will ensure safe publication of any field changes, and enforce serial usage which can
guarantee the setting that will be used (but the lock will have to be held for the set call
as well).

ChiSquareTestImpl has a non-final instance field which means its value won't necessarily be
safely published.
The field also has a setter which could be invoked by one thread while another was using it.

TTestImpl is immutable (has no fields), and OneWayAnovaImpl can be made immutable, but other
implementations of the interfaces might exist which are not immutable.

The simplest way to make the class thread-safe would be to convert all the methods and fields
from static to instance, but I don't know if that is acceptable.

> TestUtils is thread-hostile
> ---------------------------
>
>                 Key: MATH-505
>                 URL: https://issues.apache.org/jira/browse/MATH-505
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 1.2, 2.1
>            Reporter: Sebb
>
> TestUtils has several mutable static fields which are not synchronised, or volatile.
> If one of the fields is updated by thread A, there is no guarantee that thread B will
see the full update - it may see a partially updated object.
> Furthermore, at least some of the static fields reference a mutable object, which can
be changed whilst another thread is using it.
> As far as I can tell, this class must only ever be used by a single thread otherwise
the results will be unpredictable.

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message