• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Timer task

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,
Im trying to run some code on a certian time period.

If i put in like a print statment as the task it works but if a put in a method as the task it does nothing, does the timer method not allow the running of methods as tasks?

Mark
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Not sure what is wrong with yours calling methods but this works for me,



What happens when you try to call a method from the run?

John
[ February 08, 2007: Message edited by: John Bartlett ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually if i put in a method that does not pass down arguments as the task, the method will run.
But if i put in a method that takes arguments to pass down, it will not execute the method.
Im really stumped
Any ideas.
Mark
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey John,
sorry i was just adding that comment when your replied. Thanks for the input. But im still stuck in regards to a method taking arguments.
The timer is also contained in a timer method that takes arguments could that be causing a conflict?
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code i have would look similar to the below.

But it seems constructed in this way, it does not want to execute.

Mark
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i tried passing down an argument into my print method and it still worked,



I will try a few more variations and see what happens

John
[ February 08, 2007: Message edited by: John Bartlett ]
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, basically if you try to pass in params into the method call within the run method i got this error:

Cannot refer to a non-final variable [param name] inside an inner class defined in a different method

this is solved by making the params passesd into the testTimerTask method final, e.g.


Hope this helps explain why yours is probably not working.

John
[ February 08, 2007: Message edited by: John Bartlett ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i get an error, cannot refer to an non-final variabale test1 in an inner class defined in a different method.

ALso im trying to pass down an object type which contains info, so i cant pass down an argument as "hello" im afraid. Thanks for the help so far anyway john.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool seems to be on the button John,
All though the object im passing down will change so i think making it final might cause problems somewhere else.
Sure ill try and see what happens anyway.

if i get any other info ill post it up

Mark
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah kool, im not sure on the whole final param issue but from a brief scan I found this:

"when the final is used on a primitive it makes the value of the primitive immutable while when used on object references it makes the reference constant meaning that the reference can only point to only one object during its lifetime"



So from what i understand of that i think it means that you can change the contents of the Object i.e. change a variable, it just means that the reference cannot change.

please feel free to correct me if I am wrong because I am not completely sure on this.

John
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John,
It does seem to be workin now. Just not entirely as expected. Some of the objects methods work and others dont (v.strange). But sure progress is being made.

Mark
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ok, basically if you try to pass in params into the method call within the run method i got this error:

Cannot refer to a non-final variable [param name] inside an inner class defined in a different method



The reason for this compilation error, is the scope of method parameters. The testTimerTask() method can (and actually does) return, after schedulling the request, so the method parameters are no longer valid -- the stack would have been popped.

By making the local variables final, the compiler basically says... since it is final, I will make a copy (of reference) of it. The original variables may be out of scope, but I know the values to use.

Henry
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Henry, so no harm doing that so.
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi henry,

by that do you mean that a final Object can have its attributes changed, and the strange results above are caused by the method returning before the print method call in the run method?

John
 
This tiny ad is wafer thin:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic