• Post Reply Bookmark Topic Watch Topic
  • New Topic

ExecutorService with a runnable  RSS feed

 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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:
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the reference to the object can't be changed. The object itself can be changed
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!