Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need a solution to this multithreading problem  RSS feed

 
kaustuv saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am trying to implement the following logic in Java 6 :

This was my initial requirement :
1. Start a listener listening to a Tibco RV Subject
2. As I keep on getting messages, need to parse the messages and store corresponding java beans into a collection
3. Once the collection size reaches a certain predefined value, destroy the listener
4. if the whole process doesnt complete in 5 mins, send a mail through an SMTP server
5. in a never ending loop, process the collection mentioned in step 2.

This was pretty simple to implement and working fine but now I have the following changes :
1. Instead of a single subject, I have multiple Tibco RV subjects to listen to - so, multiple listeners
2. Start multiple threads each listening on a Tibco RV subject
3. Each listener thread should do the following :
3.1 as it keeps on getting messages, parse those messages into java beans and put them into a collection
3.2 when the collection size reaches a predefined value, add the collection contents to a common collection (shared across multiple listeners) and then destroy the listener
4. When the first listener thread finishes its job the main thread should continue processing the common collection in an endless loop
5. As the remaining listeners finish their jobs, the common collection would get updated and automatically processed by the main thread which is running in an infinite loop

Please suggest what should be a good solution. The number of listener threads that I need to create isnt too large (around 5-10). I am using JDK 6.

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kaustuv saha wrote:
Please suggest what should be a good solution. The number of listener threads that I need to create isnt too large (around 5-10). I am using JDK 6.


It sounds like from your description, that you already have the solution designed. In fact, it is really close to usable pseudo code. Just need to flesh out all the edge conditions, and you are good to go.

Henry
 
kaustuv saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have implemented a solution as mentioned above and it works

This is what I am doing

1. create a Hashmap (it will store subjects as key and Boolean as value to indicate job completed or not) and an arraylist(for storing received messages)
2. create an executor service pool
3. iterate over array of Tibco listener subject strings
3.1. add a key value pair to hashmap : (subject string, Boolean.FALSE)
3.2. create a runnable instance [the run() method actually starts a listener service on the subject and once the listener service is done with its job, updates the hashmap to Boolean.TRUE for that subject and adds messages received to the collection]
3.3. add the runnable to executor service
3.4. submit the executor service
4. while (in the hashmap all values as FALSE)
4.1 sleep current thread for 1 second
5. in an infinite loop go on with processing the arraylist contents. the list will get updated as executor jobs keep on getting completed


This works fine but I dont like the logic in step 4 where I am manually waiting for at least one thread to complete job.

Can anyone suggest a better way to implement this ?
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kaustuv saha wrote:
Can anyone suggest a better way to implement this ?

Look at ExecutorCompletionService:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!