• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads confusion  RSS feed

 
Ishita Saha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a problem in threads.
the requirement is that we need to maintain a list having two attributes to serve requests.
this list would contain a number of request with a delay associated with it.

the steps are:
one thread would be getting requests and pushing them to this list along with a delay component.
one other thread would be taking request from this list and server it.

now the thing is that how do we maintain this list so that it always have the element to be served right now on the top.

one doubt i have right now is that - can these two threads work in isolation. i mean do we need to have some wait & notify logic for these two threrads or we can just say that one thread keeps pushing to queue and the other keeps reading the request and serving it and like this.

please advise any possible solution ASAP
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a java.util.concurrent.DelayQueue

Elements in the queue can only be taken when their delay has expired and the most out of date element is always at the head of the queue and it's thread safe - sounds like it's just what you need.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a great match to the problem, awright.

You can also make Timer do all the work. You won't be able to see the queue, but Timer will start tasks at a scheduled time (now+delay). That's all on one thread internal to the Timer. You'd want the task to start a new thread or get one from a pool and move the real work off the Timer thread so the Timer can start the next task without waiting for others to finish.
 
Ishita Saha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies.

i guess delayedqueue is java 5 feature but i am supposed to use java 1.4 jvm only.

for timer, can you please give me some sample, may be algorithim that how do we communicate with the thread which is pushing requests, the timer thread which is poping request at (now+delay) time and the output thread which will server the request and output the results.

thanks again for your time.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!