Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
    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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 3760
536
Android 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: 13500
305
  • 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: 4281
34
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!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic