• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ExecutorService with a runnable

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a task in java that is taking a bit long, so I implemented it using thread.

My question is how do I segregate the task for an executor using an inner class?
I am using Java 1.6
Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the error message says, you cannot use a non-final variable from the enclosing method in an anonymous inner class.

The solution is simple; make the variable final:
 
Marshal
Posts: 28257
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're just asking how to fix the problem indicated by the compiler, then use a final variable like it suggests:



That's nothing to do with Map.Entry being an inner class, or with segregating a task, but if your question was meant to address those points as well, then I think more detail would be useful.
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess, I am missing the point. I understand that it can be made final, but then it's a reference to an object. Once I make it final, the reference can't be changed. Am I wrong?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the reference to the object can't be changed. The object itself can be changed
 
Paul Clapham
Marshal
Posts: 28257
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you'll observe that your posted code in fact doesn't change the reference to the object. Any other code, such as the code in the doWork() method for example, can't change the reference to the object because it doesn't have that reference. It has a copy of the reference, which of course points to the same object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic