?
Solved

Queue and singleton in Java

Posted on 2011-09-10
8
Medium Priority
?
537 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:wsyy
  • 4
  • 4
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36518166
If you have different threads only reading the queue and not changing it - there may be no issue  with  thread-safety
0
 

Author Comment

by:wsyy
ID: 36518184
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?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 36518204

If you are going to have threads some of which will be populating and others removing
data form the queue, then you probably want to use thee BlockingQueue.
Read the good article with some code examples here:

http://www.developer.com/java/ent/article.php/3645111/Java-5s-BlockingQueue.htm
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wsyy
ID: 36518787
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.
0
 

Author Comment

by:wsyy
ID: 36518806
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.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519296
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
0
 

Author Comment

by:wsyy
ID: 36519358
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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519391
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question