File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Timer task Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Timer task" Watch "Timer task" New topic
Author

Timer task

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
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

Joined: Jul 14, 2006
Posts: 146
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

Joined: Jul 14, 2006
Posts: 146
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

Joined: Jul 14, 2006
Posts: 146
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

Joined: Jan 25, 2006
Posts: 116
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

Joined: Jan 25, 2006
Posts: 116
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

Joined: Jul 14, 2006
Posts: 146
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

Joined: Jul 14, 2006
Posts: 146
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

Joined: Jan 25, 2006
Posts: 116
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

Joined: Jul 14, 2006
Posts: 146
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
thanks Henry, so no harm doing that so.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer task