[concurrency-interest] Unsynchronized lazy conditions

Alex Otenko oleksandr.otenko at gmail.com
Fri Jun 1 02:34:13 EDT 2018


I would not call the trick Effective Java.


Alex

> On 31 May 2018, at 21:06, Carl Mastrangelo via Concurrency-interest <concurrency-interest at cs.oswego.edu> wrote:
> 
> At the cost of about 500 bytes of class size, you could do the trick in Effective Java:
> 
> class Foo {
>    private static class Logged {
>      static void log() {}
>  
>       static {
>          LOG.warn("Warning");       
>       }
>    }
> 
>    public void run() {
>      Logged.log();
>    }
> }
> 
> On Wed, May 30, 2018 at 10:17 PM Shevek via Concurrency-interest <concurrency-interest at cs.oswego.edu <mailto:concurrency-interest at cs.oswego.edu>> wrote:
> Hi,
> 
> I'd like to issue a warning message a relatively low number of times in 
> a multi-threaded application. My code is this:
> 
> class Foo {
>    private boolean warned;
> 
>    public void run() {
>      if (!warned) {
>         LOG.warn("Warning");
>         warned = true;
>      }
>    }
> }
> 
> This is the only access to the variable 'warned', the value change is 
> deliberately unsynchronized, and monotonic. Am I right to believe that:
> 
> * The first call WILL issue a warning.
> * Any thread will issue the warning AT MOST once.
> * Some (later) threads may not issue the warning, if the updated value 
> is flushed to the heap and they load it?
> 
> Is there a better way to do this without sacrificing performance? Is 
> this what the mysterious AtomicBoolean.lazySet, or even 
> weakCompareAndSet is about?
> 
> This is right in the middle of something very concurrent which runs for 
> a long time, so reducing overhead is far more important than 
> occasionally issuing too many warnings.
> 
> Thank you.
> 
> S.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu <mailto:Concurrency-interest at cs.oswego.edu>
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180601/ffc2d96e/attachment.html>


More information about the Concurrency-interest mailing list