• Post Reply Bookmark Topic Watch Topic
  • New Topic

WorkerThread and work queues  RSS feed

 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.. i'm working on a corba based project and i need to have a worker thread which picks up some work(sql statements) from a queue. So i implemented a work queue class using Linked List, a worker thread etc.. did a simple test ..worked fine.. The problem is that I need to know the contents of the LinkedList bcoz i need to check during the program if a certain object is currently in the work queue or not.. so a hashmap would be helpful since i can have the objectId for example as the key and the sql statemnt associated with it as the mapping.. but then i cant do the addLast and removeFirst() operations which are very helpful. In hashmap i need to pass a particular object refrence when i say remove(key) which makes the program more complicated.. so what can i do .. any suggestions.. would really apppreciate some quick advice.. i was thinking of two solutions.
1. a data structure for the objects i put in a linked list meaning an object which contains two attributes objectID and the sql stmt but it would mean i have to call a method to check the value everytime..
OR
2. a linked list with another linked list.. but i dont know how that works. .havent used linked lists much..
So any threads and java.util expert out there.. PLEASE REPLY SOOOONNN...
thanks..
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that I need to know the contents of the LinkedList bcoz i need to check during the program if a certain object is currently in the work queue or not..
LinkedList has a method contains(Object o) which tests for membership. Is that what you are looking for?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that a LinkedHashMap may be what you want. As long as you don't use the costructor that takes a boolean, you'll get a Map that uses insertion order. Meaning every time to add something with put(), it goes to the end of the list (unless the same key was already put in the list earlier). To get the oldest mapping in the the Map, use something like:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your queue is closed to that kind of solution, you could always put something into a queue AND into a map. If it's in the map, don't put it in the queue (again). When you take it out of the queue, take it out of the map, too. This kind of thing sometimes makes people think they are holding some large object twice, but you're really only holding small references twice. Not too awful.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!