File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is a Thread class a one-run wonder?

 
Dan Kempten
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my hopper which hops in 1 second intervals. I can call 5 1 second hops with fiveHops() and it will
function fine the first time.

However the second time I call fiveHops() it has an java.lang.IllegalThreadStateException

for timing reasons I don't want to call hopper = new Hopper() each time I call fiveHops()

How can the hopper object be reset in sufficient manner to stop calling this exception??

is a Thread object meant to always be used once and then discarded??


 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadocs of the java.lang.Thread#start() method: It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Why don't you want to create 4 additional objects? Object creation and the resulting memory management will likely have a performance impact that is not even measurable.
 
Sam Epple
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip Ulf.
It is measurable. 13 ms of time spent doing new Hopper()
when doing a 50 ms hop is a 26% bite.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you have to come up with a different scheme. You can't re-start the same thread. You could make Hopper not-a-Thread, and use a single thread that waits on a signal that it should go (then re-send the Hopper to run as that signal.) This can be done a number of ways, such as through TimerTasks, or ThreadPoolExecutor (or ScheduledThreadPoolExecutor).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic