Link to home
Start Free TrialLog in
Avatar of wsyy
wsyy

asked on

Queue and singleton in Java

Hi,

I want to implement a singleton queue class, which will be accessed by multiple threads.

The queue contains no more than 10000 of files. Most likely files with same prefix will be put together in the queue, but not guaranteed. I would like to have multiple thread read through the queue, identify a group of files of that kind.

I heard thread-safe is difficult to implement for queue, and would like some experts' views, suggstions and for the best, some code.

Thanks
Avatar of for_yan
for_yan
Flag of United States of America image

If you have different threads only reading the queue and not changing it - there may be no issue  with  thread-safety
Avatar of wsyy
wsyy

ASKER

how to implement it though, for_yan. do you have some code?

I forgot to mention that after reading and grouping such files, they should be taken off the queue. will that be considered challenging?
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wsyy

ASKER

for_yan, thanks for the link.

I need to put in the BlockingQueue the files to be processed. The putting process is not multi-threading.

I also need to get the files off the queue, which however is multi-threading.

How can I modify the cited code example to achieve my goals?

In the example, I only see in the Client.java how to submit a request to the Server.java. I nonetheless see a process to identify, group, and do other operation in the queued requests.

Please help.
Avatar of wsyy

ASKER

for_yan,

Do I need to develop two kinds of requests?

One is the putting request (or the producer request), and the other is the getting request (or the consumer request).

The cited example seems to not distinguish the two requests.
BlockingQueue has different options for your possible needs  - method put will be blocking till the space in
the BlockingQueue becomes avaialabe - this is for queues with limited number of entries.
If you don't have limit on the number of entries in the queue you may use method offer which
will add element anyway.
Method take() will be blocking until the elements arrived if there is nothing in the BlockingQueue object - I
think you need to use this method.

As I understand you need to put file names to BlockingQueue not the files themselves.
There is also Iterator method which will not affect the contents of the queue but whih would allow you to analyze the contetnts
of your queue but does not affect the contents of it say before you decide what to do with the elements of this queue
Avatar of wsyy

ASKER

for_yan:

How can I start the server at the very beginning so that all the clients can call it and don't have to regenerate a new server instance?
This is separate issue specific to your situation - usually there is no issue in generating a new
server instance for eachj clinet - this server instance is just a handle to the server and if the clients are separate,
and say running on different machines they may each need such seprate handle.
Say, if you are running RMI server (Remote Method Invoication) server then you start server on one machine - if you have clinest running
in separate JVM (usually even on different computers) then each clinet will lookup the server through the registry get the handle to that server instance and use it
to invoike the method on the server. If you want to run several methods from withi one java application you can
have a statoc variable for the server instance and use one server instance to invoke all methods in that application,
but if each one uses it own instance it is also not a big deal.
So how you set up interactions between your clients and the server - this is a separate issue.