Effectively final

Brian Goetz brian.goetz at oracle.com
Wed Sep 14 19:11:12 PDT 2011


A final reference to thread-safe mutable state is fine, like AtomicBoilean.   What is being captured is the reference, which is final. What will get you fired is using an array, which is not thread safe. 

Sent from my iPhone

On Sep 14, 2011, at 12:48 PM, Serge Boulay <serge.boulay at gmail.com> wrote:

> not even through a reference such as an atomic boolean ? (yes, I know Brian
> frowns on this stuff)
> 
> void foo(final Socket socket) {
>     AtomicBoolean timedOut = new AtomicBoolean(false);
>     system.setTimeout(100, ->{ timedOut.set(true); });
>     socket.onData(->{ if (!timedOut.get()) System.out.println("Got data");
> });
> }
> 
> On Wed, Sep 14, 2011 at 2:57 PM, Neal Gafter <neal at gafter.com> wrote:
> 
>> A lambda can't modify a local variable from the enclosing scope.
>> 
>> On Wed, Sep 14, 2011 at 11:46 AM, blackbelt1999 <
>> blackbelt1999 at sbcglobal.net
>>> wrote:
>> 
>>> Is there not a fundamental difference between the original code and the
>> new
>>> code, namely the original code creates a new thread of execution while
>> the
>>> new
>>> code does not? If so, can't the new code be simplified to:
>>> 
>>>  void foo(final Socket socket) {
>>>       boolean timedOut = false;
>>>      system.setTimeout(100, () =>  { timedOut = true; });
>>>     socket.onData(() =>  { if (!timedOut) System.out.println("Got
>> data");
>>> });
>>>  }
>>> 
>>> The above version .does not go through the expense of creating a new
>> object
>>> while providing the same behavior?
>>> 
>>> --Alan
>>> 
>>> 
>>> 
>>> 
>>> ________________________________
>>> From: Steven Simpson <ss at comp.lancs.ac.uk>
>>> To: lambda-dev at openjdk.java.net
>>> Sent: Wed, September 14, 2011 4:10:42 AM
>>> Subject: Re: Effectively final
>>> 
>>> Just a loose end...
>>> 
>>> On 15/08/11 15:41, Tim Fox wrote:
>>>> On 15/08/2011 15:22, Steven Simpson wrote:
>>>>>      void foo(final Socket socket) {
>>>>>        new Runnable() {
>>>>>          boolean timedOut;
>>>>>          public void run() {
>>>>>            system.setTimeout(100, #{ timedOut = true; });
>>>>>            socket.onData(#{ if (!timedOut) System.out.println("Got
>>> data");
>>>> });
>>>>>          }
>>>>>        }.run();
>>>>>      }
>>>> Steven, firstly thanks for putting in the effort to look at this. And
>>>> kudos for your ingenuity :)
>>> 
>>> And yet I missed this trick!:
>>> 
>>>  void foo(final Socket socket) {
>>>    new Object() {
>>>      boolean timedOut;
>>>      void run() {
>>>        system.setTimeout(100, () ->  { timedOut = true; });
>>>        socket.onData(() ->  { if (!timedOut) System.out.println("Got
>>> data");
>>> });
>>>      }
>>>    }.run();
>>>  }
>>> 
>>> So the object doesn't have to be Runnable, and run() doesn't have to be
>>> public, return void, or be called run!  That's more convenient if you
>>> want to return a value, rather than doing tricks with Callable:
>>> 
>>>  return new Object() {
>>>    int run() { return 0; }
>>>  }.run();
>>> 
>>> Cheers,
>>> 
>>> Steven
>>> 
>>> 
>> 
>> 
> 


More information about the lambda-dev mailing list