Solved

Queue and singleton in Java

Posted on 2011-09-10
8
463 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 250 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now