Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

ExecutorService must be final?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
By testing the sample questions at Wiley, I noticed that the IDE complains that ExecutorService must be final or effectively final, so that all the examples in our book must make a temp final copy of previous 'service' to let it compile and run?

 
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

enar wang wrote:By testing the sample questions at Wiley, I noticed that the IDE complains that ExecutorService must be final or effectively final, so that all the examples in our book must make a temp final copy of previous 'service' to let it compile and run?



If you are using "service" inside a lambda in the forEach statement, then it must be effectively final. You initialized "service" to null, and then you assign it to Executors.newSingleThreadExecutor(), so that means it is no longer effectively final.
 
Marshal
Posts: 4400
567
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

enar wang wrote:By testing the sample questions at Wiley, I noticed that the IDE complains that ExecutorService must be final or effectively final, so that all the examples in our book must make a temp final copy of previous 'service' to let it compile and run?


If this is a test question (rather than an exercise), then maybe it is not supposed to compile??
 
Saloon Keeper
Posts: 15259
349
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Agree with Edmund. This has nothing to do with the class ExecutorService. You're not allowed to write lambda expressions or anonymous classes that close over variables that are not effectively final. The reason for this is that the compiler doesn't know when the function will get executed, so it needs a fixed value to ensure that the closure agrees with what you expect.

You can easily solve your problem by assigning your service variable just once. There is no point in calling newSingleThreadExecutor() inside the try-block, because either it succeeds and you can start the try-finally statement immediately afterwards, or it fails and you don't have anything to shut down:
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I really wish ExecutorService supported AutoCloseable.... then you could use it in a try-with-resources statement.  Since it does not, it is strongly recommended in practice you wrap it in try/finally block if you're not going to keep the service open after the method finishes.

On the exam, they rarely do this but it's a good practice in real life.
 
enar wang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thank you for all replies!
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic