File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes need a solution to this multithreading problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "need a solution to this multithreading problem" Watch "need a solution to this multithreading problem" New topic
Author

need a solution to this multithreading problem

kaustuv saha
Greenhorn

Joined: Dec 14, 2009
Posts: 22
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.


Certifications: SCJP (2005), SCWCD (2006), OCA (2006), WAS Admin (2007), SCBCD (2008), SCJD (2009), SCDJWS(2009)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13408

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kaustuv saha
Greenhorn

Joined: Dec 14, 2009
Posts: 22
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 ?

This message was edited 2 times. Last update was at by kaustuv saha

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 413
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
 
 
subject: need a solution to this multithreading problem
 
developer file tools