File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Threads vs java.util.Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads vs java.util.Timer" Watch "Threads vs java.util.Timer" New topic
Author

Threads vs java.util.Timer

Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Working on an application right now that runs from the command line but will be a GUI in the future. It will connect to a sensor (probably for weeks at time) and gather information, I have 5 tasks that may run once a minute, every 5 minutes or at greater intervals depending on a user's preference. The tasks, would get the current sensor data and push to a web site, or ftp, or push to a database.

It looks like threads is the right solution but I wonder if java.util.Timer would work as well. There might be times where the tasks need to run at 5 minutes on clock so 5, 10, 15, etc. and I thought Timer would be good for this but I could not tell if I could share a data object with the timer task.

Also, I am guessing there would be overhead to the startup of each of these tasks each time using Timer and went back to the Thread approach.

So I am looking for guidance on approach and hope I have provided enough information to help a Greenhorn along.

Thanks
Mike
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

If it's easier to program (and it looks like it would be) then use java.util.Timer. (There's no "threads" alternative, actually, because the Timer uses a thread itself to run the scheduled code.)

And I wouldn't worry about a few microseconds of startup overhead for something which is going to run every 5 minutes (which is 300,000,000 microseconds).
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

For more sophisticated usages you can use the alternative solution i.e.: http://www.quartz-scheduler.org/

Adam


SCJP, SCWCD, SCBCD, SCDJWS
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Thanks for the responses. Is there a way to pass a data object to the Timer when it kicks off the way you share data with a Thread? The basic research I have done does not show that or I miss understood the examples.

I'll at quartz as well.

Mike
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Mike Bates wrote:Is there a way to pass a data object to the Timer when it kicks off the way you share data with a Thread?


Well, it is not the Timer that needs the data object, it is the Timer task. How about passing it as a parameter to the task's constructor?

Henry


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

Joined: Sep 19, 2009
Posts: 81
Thanks for the push. I should have thought of that. Now I am off to build a test case.

Mike
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
I built a test case last night (actually borrowed some code examples) and found that with timer the task basically takes control from the main part of the program.

So in one case, I am concerned where I am making a telnet connection sending some data and closing but the time it takes is 23 seconds which is very long and would cause sensor data to be lost. The interface with telnet is mostly waiting for a response from the server and I have no control over that interface. So would threads improve this or would I have the same issue. I am making a basic assumption that threads would run in parallel and thought Timer tasks would run the same way.

May be I am approaching this wrong.

Mike
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

A java.util.Timer only has one thread. So, you are have long running tasks, instead of short (either repeating or not) running tasks, they will be waiting for their turn.

If your task is really long running, it may be worth it to give it a dedicated thread... Also, if you want a Timer with multiple threads, aka. thread pools, take a look at the java.util.concurrent.Executors class instead.

Henry
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
I started playing with a simulated set of threads with one or two that are long running, one just constant one, and a couple of shorter ones. That seems to spread the load out a bit, now to put it into a real world scenario. It will be interesting to see what it does to the CPU use, I think I'll see some peak usage at certain times but that would be normal. I am gong to take a look at java.util.concurrent.Executors class to set this up as well.

Thanks
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads vs java.util.Timer