Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using a thread inside a context listener  RSS feed

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a context listener designed to update some database records and a context attribute (bean) every 10 minutes.

My question is regarding lines 62-67. At the time it seemed like a good idea for garbage collection purposes, but this is inside a thread that runs in a continuous loop so maybe I should leave those objects alone. I'm not strong on threading so I don't fully understand the implications of this. Am I causing them to be destroyed and recreated every 10 minutes? And if so, is that a bad idea or a good thing?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can ignore the threading part in it. The important piece in that code is that the variables which you are setting to null are all method local variables. Those method local variables will lose their scope when the method completes and will be eligible for garbage collection. You don't have to explicitly set them to null. It's semantically the same as having a loop which call the run() method on the same instance, N number of times.

I think you might be able to visualize it better if you moved that Runnable anonymous class into a separate top level class of its own.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:It's semantically the same as having a loop which call the run() method on the same instance, N number of times.

Okay, that helps me understand it better.

Jaikiran Pai wrote:I think you might be able to visualize it better if you moved that Runnable anonymous class into a separate top level class of its own.

Hmmm. Now that's interesting. When I was researching how to do this timer thing, all the examples I found did it as an anonymous inner class. I'll have to think about how it would be done as a top level class.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
Jaikiran Pai wrote:I think you might be able to visualize it better if you moved that Runnable anonymous class into a separate top level class of its own.

Hmmm. Now that's interesting. When I was researching how to do this timer thing, all the examples I found did it as an anonymous inner class. I'll have to think about how it would be done as a top level class.


Let's take a look at line 71 in your code:



What it's expecting is an instance of type Runnable interface as the first parameter to that method. In your code and the examples you are referring to, the Runnable interface is implemented as an anonymous class. You can just have any other top level class implement that Runnable interface and then create an instance of it to pass it to the scheduleAtFixedRate method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!