?
Solved

Queue and singleton in Java

Posted on 2011-09-10
8
Medium Priority
?
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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