• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i avoid threads here?  RSS feed

 
Natraj Gudla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to maintain a time count on several objects of the same class, to proceed for further actions on the object when it reaches its time,
now can i do something like the object on its own starts off its action calls when it reaches the time set...periodically..without that object being a thread..
also i can maintain a for loop to run thru checking for the time of each object , but in that way it will be serial execution and i may not be exact in executing some actions when it is needed for an object..as i am in one iteration ,i may be missing the time for some other object somewhere to come after some more iterations...
that is the reason can i have some thing in a way that each object can check its own time and call the actions , without them being threads...

can anyone help me?..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you not want to use threads? If it's just that they seem too hard to use, don't worry; they're not, really. You might use java.util.Timer to wait for a set period and then execute some code; Timer will use a Thread internally, without your worrying about it explicitly.
If you've got some restriction that prevents you from using threads, then I'm afraid your for-loop (i.e., polling the objects) is the only choice you've got.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You express concern about one task running long enough to overlap with the start time of the next. If this is a possibility, you pretty much have to run your tasks on their own threads. Before I read about Timer in the JDK I wrote my own scheduler. Given a collection of tasks with specified times, it finds the earliest time in the collection and does a wait() until that time, then executes the task and removes it from the collection and waits for the next one.
You can make your tasks Commands, ie implement a simple interface with a single execute() method. Commands that believe they will run fast enough could just do their work on the invoker's thread. Others could implement runnable and start themselves on new threads.
Hope that helps!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!