• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can i avoid threads here?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?..
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic