Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads vs java.util.Timer

 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

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

Adam
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the push. I should have thought of that. Now I am off to build a test case.

Mike
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic